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

completed 4.4 - carmen #7

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@

rect.getPerimeter(); // 140
rect.getArea(); // 1200

```



15 changes: 13 additions & 2 deletions short-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

console.log(b);
```
*When the 'func' fucntion is invoked the b variable becomes a apart of the global scope. So, when the varibale 'b' is logged it will return what value the variable 'b' was assigned with, which is 1.


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

console.log(context);
```
3. What will the code below output? Explain the difference, if any, between this output and that of problem 8.
*This will log the global object. This is because when the 'func' function is invoked in the 'context', 'this' is returned and 'this' is refering to 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 +38,7 @@

console.log(context);
```
*This will return the object 'obj' with the function 'func', because the function is invoked in the variable 'context' as a method of the 'obj' object.

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 +61,17 @@

console.log(computer.total());
```
*This was returning 3500, because 'this.price' in the function 'specialDiscount' is not refering to the 'price' property in the 'computer' object. However, if you

5. What is a closure?
*A closure is a function declared inside of a fucntion, that has access to the outer function's scope.

6. What are the benefits of using a constructor functions to create object instances instead of a factory function?
*The main benefits would be that by using constructor functions to create objects, is that you can have a constructor that has properties and methods you want multiple objects to have. This means you do not have to repeatedly create the same properties you want multiple objects to have when you create them, you just have to create a 'new' object from your constructor. This also means you can maintian methods in one construcor for multiple objects.

7. What is the `__proto__` property and how does it differ from the `protoype` property?
*The `__proto__` is a property that points to the object's protoype object. It differs from the protoype, because protoype is the object used to build `__proto__`, and it is only availble on functions while `__proto__` is available everywhere.


8. What is inheritance?
8. What is inheritance?
*Inheritance is when parent classes pass down properties and methods to child classes, but child classes being seperate from the parent class.
82 changes: 82 additions & 0 deletions solutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// problem 1
function makeCounter(startingValue){
return function(){
return startingValue += 1;
}
}

// problem 2
function makeFriendList(){
let arr = [];
return {
addFriend: function(name){
arr.push(name);
return `${name} successfully added.`

},
removeFriend: function(name){
if(arr.includes(name)){
arr.splice(arr.indexOf(name), 1);
return `${name} successfully removed.`
}else{
return `${name} not found.`
}
},
displayFriends: function(){
return arr;
}

}
}

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

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

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

// problem 4 &\ 5
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);
}


function Rectangle(length, width) {
Quadrilateral.call(this, length, width);
this.length1 = length;
this.width1 = width;
this.side3 = length;
this.side4 = width;
}

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

Rectangle.prototype.getArea = function() {
return this.side1 * this.side2;
}

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