From 9898fb74719ec578eb6511c2b328e4753f8832e6 Mon Sep 17 00:00:00 2001 From: MINE Date: Wed, 21 Mar 2018 14:01:51 -0700 Subject: [PATCH 1/3] Completed Recursion --- recursion.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/recursion.js b/recursion.js index b4b66d1..be0c98f 100644 --- a/recursion.js +++ b/recursion.js @@ -10,9 +10,19 @@ while (n <= 10) { // write a recursive - function called countToTen that mimics the while loop above. // code here +const countToTen = (num = 1) => { + console.log(num); + if (num === 10) { + return ""; + } else { + return countToTen(num + 1); + } +} // when you code is ready, un-comment the next line and run the file -// console.log(countToTen()); +console.log(countToTen()); + + /* ================ Next Problem ================= */ // Problem 2: @@ -29,5 +39,12 @@ console.log(factorial(5)); // write the above function in a recursive way. +const recursiveFactorial = num => { + if (num === 1) { + return 1; + } + return num * recursiveFactorial(num - 1); +}; + // when your code is ready, un-comment the next line and run the file -// console.log(recursiveFactorial()); +console.log(recursiveFactorial()); From 85903d1f0ceff6cca0f35a8cbdb43299409d6269 Mon Sep 17 00:00:00 2001 From: MINE Date: Thu, 22 Mar 2018 12:57:36 -0700 Subject: [PATCH 2/3] Completed Constructors & Classes --- classes.js | 23 ++++++++++++++++++----- constructors.js | 18 ++++++++++++------ this.js | 16 ++++++++++++++-- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/classes.js b/classes.js index c275caa..4489d10 100644 --- a/classes.js +++ b/classes.js @@ -3,17 +3,30 @@ // problem #1 // convert the Animal constructor function from 'constructors.js' into an ES6 class +class Animal { + constructor(options) { + this.name = options.name; + } + grow() { + return `${this.name} grew larger!`; + } +} // problem #2 // convert the Cat constructor function from 'constructors.js' into an ES6 class +class Cat extends Animal { + constructor(catOptions) { + super(catOptions); + } +} // if everything is setup properly the code below will print 'Foofie grew larger!' // uncomment the code below to test your solution -// const foofie = new Cat({ -// name: 'foofie', -// }); -// -// foofie.grow(); +const foofie = new Cat({ + name: 'foofie', +}); + +foofie.grow(); diff --git a/constructors.js b/constructors.js index d0c9de2..8a17b2a 100644 --- a/constructors.js +++ b/constructors.js @@ -9,6 +9,9 @@ function Animal(options) { } // add 'grow' to Animal's prototype here +Animal.prototype.grow = function() { + return `${this.name} grew larger!`; +} // problem #2 // setup Cat to inherit from Animal @@ -17,17 +20,20 @@ function Animal(options) { // instances of Cat should also have access to the 'grow' method function Cat(options) { - // invoke Animal here with .call + Animal.call(this, options); } +// invoke Animal here with .call + +Cat.prototype = Object.create(Animal.prototype); // connect the prototypes here // if everything is setup properly the code below will print 'Foofie grew larger!' // uncomment the code below to test your solution -// const foofie = new Cat({ -// name: 'foofie', -// }); -// -// foofie.grow(); +const foofie = new Cat({ + name: 'foofie', +}); + +foofie.grow(); diff --git a/this.js b/this.js index 895b5fe..700543a 100644 --- a/this.js +++ b/this.js @@ -2,8 +2,20 @@ * in your own words. explain the four principle for the "this" keyword below. * * 1. -* 2. -* 3. + +* 2. Implicit Binding - Invoking the object left of the dot +const dog = { + name: "Kenzie", + breed: "collie", + color: "black", + voice: function() { + console.log(this.breed); + } +} +dog.breed(); + +* 3. + * 4. * * write out a code example of each explanation above From 77d5494b264e3937ad8dc49c6c72e3245d04ebff Mon Sep 17 00:00:00 2001 From: MINE Date: Thu, 22 Mar 2018 13:09:40 -0700 Subject: [PATCH 3/3] Completed This --- this.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 12 deletions(-) diff --git a/this.js b/this.js index 700543a..7b48cae 100644 --- a/this.js +++ b/this.js @@ -1,22 +1,13 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. +* 1. Explicit Binding - Using the context of what the keyword should be Call, Apply or Bind * 2. Implicit Binding - Invoking the object left of the dot -const dog = { - name: "Kenzie", - breed: "collie", - color: "black", - voice: function() { - console.log(this.breed); - } -} -dog.breed(); -* 3. +* 3. New Binding - A given constructor will invoke a brand new Object in alphabetical order. Or when the new function in invoked with new keyword, that keyword inside the function is bond to the new Object being constructed -* 4. +* 4. Window Binding - Referencing a keyword inside the function, it will default to the 'window' keyword object * * write out a code example of each explanation above */ @@ -27,14 +18,98 @@ console.log('hello world!'); // code example for Window Binding +var sayAge = function() { + console.log(this.age); + }; + var me = { + age: 25 + }; + +sayAge(); +window.age = 35; +sayAge(); +//output - undefined +// 35 + // Principle 2 // code example for Implicit Binding +const dog = { + name: "Kenzie", + breed: "collie", + color: "black", + voice: function() { + console.log(this.breed); + } +} +dog.breed(); +//output "collie" + + // Principle 3 // code example for New Binding +var Dog = function(breed, age, color) { + this.breed = breed; + this.age = age; + this.color = color + }; + +var Kenzie = new Dog('collie', '8', 'black'); + console.log(Kenzie); + +// output [object Object] { +// age: “8”, +// breed: “collie”, +// color: “black” + // Principle 4 // code example for Explicit Binding + +// CALL +var sayName = function(lang1, lang2, lang3) { + console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3); + }; + +var amanda = { + name: 'Amanda', + age: 18, +}; + +var languages = ['Javascript', 'Ruby', 'Flexbox']; + +sayName.call(amanda, languages[0], languages[1], languages[2]); +//output “My name is Amanda and I know Javascript, Ruby, and Flexbox” + +// APPLY +var sayName = function(lang1, lang2, lang3) { + console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3); + }; +var amanda = { + name: 'Amanda', + age: 18, + }; +var languages = ['Javascript', 'Ruby', 'Flexbox']; + +sayName.apply(amanda, languages) +//output “My name is Amanda and I know Javascript, Ruby, and Flexbox” + + +// BIND +var sayName = function(lang1, lang2, lang3) { + console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3); + }; +var amanda = { + name: 'Amanda', + age: 18, + }; +var languages = ['Javascript', 'Ruby', 'Flexbox']; + +var newFunction = sayName.bind(amanda, languages[0], languages[1], languages[2]) +console.log("NOW"); +newFunction(); +//output "NOW" +//output "My name is Amanda and I know Javascript, Ruby, and Flexbox" \ No newline at end of file