Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polished answers and repushed to fresh repo. #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
19 changes: 18 additions & 1 deletion short-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

console.log(b);
```
It logs 1 because b is assigned the value in the global scope when the function is called,
so when the is logged, it prints 1.

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

console.log(context);
```
It returns the Window object, because this is in the global scope.

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 +38,9 @@

console.log(context);
```
A property is assigned to a function inside the object, and storing that function to the variable context. Instead of the window object being returned,
we get the value of func, which in this case is a function.
This is because it 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 @@ -56,11 +63,21 @@

console.log(computer.total());
```
Change specialDiscount() from a function declaration to an arrow function,
`this` will the be bound the object fixing the issue.

5. What is a closure?
A closure is a function inside a function.
It hides 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?
The ability create methods in prototype, which will apply to the constructor object as well as all the objects that are constructed with it is a benefit of using constructor functions.
You can also create new objects and have variables point to the object you create through them.

7. What is the `__proto__` property and how does it differ from the `protoype` property?

8. What is inheritance?
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?
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.
74 changes: 74 additions & 0 deletions solutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//Question 1
const makeCounter = function(start) {
return function() {
start++;
return start;
}
};

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

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