diff --git a/exercises.md b/exercises.md index b9012c6..b030746 100644 --- a/exercises.md +++ b/exercises.md @@ -66,6 +66,8 @@ rect.getPerimeter(); // 140 rect.getArea(); // 1200 + ``` + diff --git a/short-response.md b/short-response.md index 1b57dbc..eea3512 100644 --- a/short-response.md +++ b/short-response.md @@ -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 @@ -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() { @@ -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 @@ -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? \ No newline at end of file +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. \ No newline at end of file diff --git a/solutions.js b/solutions.js index e69de29..706e7fe 100644 --- a/solutions.js +++ b/solutions.js @@ -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, +} \ No newline at end of file