Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const countFromFive = makeCounter(5);
countFromFive(); // 6
countFromFive(); // 7
countFromFive(); // 9
countFromFive(); // 8

const countFromOneHundred = makeCounter(100);
countFromOneHundred(); // 101
Expand Down
11 changes: 10 additions & 1 deletion short-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

console.log(b);
```
This logs 1, since the variable wasn't declared with any keyword it turned into a property in the global object which it's value can be accessed from anywhere.

2. What does the following code log? Why?
```javascript
Expand All @@ -22,6 +23,8 @@

console.log(context);
```
This logs "window" since during run time the current execution context is the global object.

3. What will the code below output? Explain the difference, if any, between this output and that of problem 8.
```javascript
const obj = {
Expand All @@ -34,6 +37,7 @@

console.log(context);
```
This will output "obj" since the execution context during run time is the object which the property's value belongs to.

4. We expect the following code to log `34000` but instead we are getting `35000`. What is the bug and how can we fix it?
```javascript
Expand All @@ -56,11 +60,16 @@

console.log(computer.total());
```
We keep getting 3500, because when the inner function specialDiscount is invoked the execution context during run time is the global object. The global object doesn't have a property named "price" who's value is greater than 20000, so it will return 0 as the specialDiscount. When the outer function returns the properties "price", "shipping" then "this" is referring to the object computer. The object does have the properties "price" and "shipping", so it will add those values with the value of tax and what specialDiscount returns when called. One way to fix this is to change "this" in the inner function to "computer"

5. What is a closure?
A closure is an inner function that gives you access to the conten in the outer function, and do something with that data to create something

6. What are the benefits of using a constructor functions to create object instances instead of a factory function?
The benefits of using a constructor instead of a factory function is that with constructors we can create object instances that inherit all properties and methods from the constructor while with the factory function you are returning and creating a new object which can't read the execution context "this"

7. What is the `__proto__` property and how does it differ from the `protoype` property?
A __proto__ is the prototype that an object instances inherites methods and properties from the class constructor prototype properties and methods

8. What is inheritance?
8. What is inheritance?
Inheritance is when object instances get all the properties and methods from the constructor they were created from
79 changes: 79 additions & 0 deletions solutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 1)
const makeCounter = function(startingValue){
return function(){
return startingValue += 1;
console.log(startingValue += 1);
}
}

//2)
function makeFriendList(){
const friends =[];
return {
addFriend: function(string){
friends.push(string);
return `${string} successfully added.`
},
removeFriend: function(string){
if(friends.includes(string)){
friends.splice(friends.indexOf(string),1);
return `${string} successfully removed.`
}
return `${string} not found`;
},
displayFriends: function(){
return friends;
}
}
}

// 3)
function Teacher(name, school, grade, subject) {
this.name = name;
this.school = school;
this.grade = grade;
this.subject = subject;
this.students = [];
}

Teacher.prototype.addStudent = function(name){
this.students.push(name);
return this.students.length;
};

Teacher.prototype.changeSchools = function(school){
this.school = school;
return this.school;
};


//4)
function Quadrilateral(side1, side2, side3, side4) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
this.side4 = side4;
}

Quadrilateral.prototype.getPerimeter = function (){
return this.side1 + this.side2 + this.side3 + this.side4;
};

//5)
function Rectangle(length, width){
Quadrilateral.call(this, length, width, length, width);
this.getArea = function (){
return this.side1 * this.side2;
};
}

Rectangle.prototype = Object.create(Quadrilateral.prototype);
Rectangle.prototype.constructor = Rectangle;

module.exports = {
makeCounter,
makeFriendList,
Teacher,
Quadrilateral,
Rectangle,
};