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
8 changes: 4 additions & 4 deletions exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
```javascript
const countFromFive = makeCounter(5);
countFromFive(); // 6
countFromFive(); // 7
countFromFive(); // 9
countFromFive(); // 7
countFromFive(); // 8

const countFromOneHundred = makeCounter(100);
countFromOneHundred(); // 101
Expand All @@ -15,8 +15,8 @@
```

2. Create a function called , `makeFriendList`, that returns an object that mutates a private array. The object should have three methods - `addFriend`, `removeFriend`, `displayFriends`.
* `addFriend` takes a string argument and adds that arguments the private array of friends. `addFriend` should return `"<<friend>> successfully added."`.
* `removeFriend` should take a name and remove that name from the private friend array. If found, it should return `"<<friend>> successfully removed."`. If not found, it should return `"<<friend>> not found."`.
* `addFriend` takes a string argument and adds that arguments the private array of friends. `addFriend` should return `"<<friend>> successfully added."`.
* `removeFriend` should take a name and remove that name from the private friend array. If found, it should return `"<<friend>> successfully removed."`. If not found, it should return `"<<friend>> not found."`.
* Lastly, `displayFriends` should return the private friends array.

3. Create a constructor function, `Teacher`. Objects created with the `Teacher` function should have the following properties:
Expand Down
15 changes: 12 additions & 3 deletions short-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

console.log(b);
```
- It logs 1 because you're assigning the variable `b` to 1 in the global scope by calling the function, so when you log the variable, it gives you 1.

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

console.log(context);
```
3. What will the code below output? Explain the difference, if any, between this output and that of problem 8.
- It returns the Window object, because `this` is just another keyword to say `window`, which is the global object.

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

console.log(context);
```
- You're assigning a property to a function inside an object, and storing that function to the variable `context`. Instead of returning the `windows` object, we get the value of `func`, which is a function. That's because `this` is not pointing to `window` anymore - it's inside the context of `obj`.

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 @@ -42,7 +46,7 @@
shipping: 2000,
total: function() {
var tax = 3000;
function specialDiscount() {
const specialDiscount = () => {
if (this.price > 20000) {
return 1000;
} else {
Expand All @@ -56,11 +60,16 @@

console.log(computer.total());
```
- Turn the function declaration for specialDiscount() into an arrow function, so the `this` becomes lexically binded to the object.

5. What is a closure?
- A closure is a function inside a function. It provides more privacy for variables since the outer function creates a scope for the inner function.

6. What are the benefits of using a constructor functions to create object instances instead of a factory function?
- With constructor functions, you can create methods in prototype, which will apply to the constructor object as well as all the objects that are constructed with it. You can create new objects and have variables point to the object you create.

7. What is the `__proto__` property and how does it differ from the `protoype` property?
- Prototype is a property of a function, while __proto__ is an object that points to the prototype and also lists things like the constructor and methods that can be used for all objects.

8. What is inheritance?
8. What is inheritance?
- Inheritance is when an object that is created by a constructor takes in parameters or values from the constructor object and uses it in itself. Inheriting methods, for example, takes the methods of the constructor prototype, which allows the object to use the methods as well.
73 changes: 73 additions & 0 deletions solutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Question 1
const makeCounter = function(startingValue) {
return function() {
startingValue++;
return startingValue;
}
};

//Question 2
const makeFriendList = function(friendArr) {
return {
addFriend(name) {
friendArr.push(name);
return `${name} successfully added.`
},
removeFriend(name) {
if(friendArr.includes(name)) {
friendArr.splice(friendArr.indexOf(name), 1);
return `${name} successfully removed.`
}
return `${name} not found.`
},
displayFriends() {
return friendArr;
}
}
}

//Question 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(studentName) {
this.students.push(studentName);
return this.students.length;
}

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

//Question 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);
}

//Question 5
function Rectangle(length, width) {
Quadrilateral.call(this, length, width);
this.length = length;
this.width = width;
this.side3 = length;
this.side4 = width;
}

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

Rectangle.prototype.getArea = function() {
return this.length * this.width;
}