diff --git a/classes.js b/classes.js index c275caa..613003f 100644 --- a/classes.js +++ b/classes.js @@ -2,18 +2,28 @@ // problem #1 // convert the Animal constructor function from 'constructors.js' into an ES6 class - +class Animal { + constructor(options) { + this.name = options.name; + } + grow() { + console.log(`${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(); \ No newline at end of file diff --git a/constructors.js b/constructors.js index d0c9de2..f3241cc 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() { + console.log(`${this.name} grew larger!`); +}; // problem #2 // setup Cat to inherit from Animal @@ -17,17 +20,18 @@ 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); } // connect the prototypes here +Cat.prototype = Object.create(Animal.prototype); // 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/recursion.js b/recursion.js index b4b66d1..496bcc0 100644 --- a/recursion.js +++ b/recursion.js @@ -9,10 +9,28 @@ while (n <= 10) { // write a recursive - function called countToTen that mimics the while loop above. -// code here +const countToTen = n => { + let n = 1; + if (n > 10) { + return; + } + console.log(n); + return countToTen(++n); +}; +countToTen(1); +/*******************************/ +let n = 1; +const countToTen = () => { + if (n > 9) { + return 10; + } + console.log(n); + return countToTen(n++); +}; +console.log(countToTen()); + + -// when you code is ready, un-comment the next line and run the file -// console.log(countToTen()); /* ================ Next Problem ================= */ // Problem 2: @@ -29,5 +47,13 @@ console.log(factorial(5)); // write the above function in a recursive way. -// when your code is ready, un-comment the next line and run the file -// console.log(recursiveFactorial()); +const factorial = n => { + //base case + if (n === 0) { + return 1; + } + //return function + return n * factorial(n - 1); +}; + +console.log(factorial(5)); \ No newline at end of file diff --git a/this.js b/this.js index 895b5fe..a8d328a 100644 --- a/this.js +++ b/this.js @@ -1,28 +1,72 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. -* -* write out a code example of each explanation above +* 1. Implicit binding - when a function containing the THIS keyword is invoked, + look to the left of the . (period) to see what object THIS is referring to + +* 2. Explicit binding - explicitly state what the THIS keyword refers to by using .call / .apply / .bind + .call arguments must be passed one-by-one or via spread syntax (...argumentsArray) + .apply allows us to pass an array as a list argument (array) + .bind creates a new function for future use + +* 3. new Binding - utilizes the NEW keyword to invoke a function which creates a new THIS object + +* 4. window Binding - defaults the THIS keyword to the window object + */ -console.log('hello world!'); +/************************************************/ // Principle 1 - // code example for Window Binding +const heyThere = function() { + console.log('hey, ' + this.name); +} +const me = { + name: 'jeffrey', + age: 24 +}; + +heyThere(); //returns 'hey, ' +window.name = 'jeffrey'; +heyThere(); //returns 'hey, jeffrey' + +/************************************************/ // Principle 2 +// code example fo Implicit Binding +const Person = function(name, age) { + return { + name: name, + age: age, + sayHi: function() { + console.log('hey, ' + name); + } + } +}; +const me = Person('jeffrey', 24); +me.sayHi(); -// code example for Implicit Binding +/************************************************/ // Principle 3 - // code example for New Binding +const Person = function(name, age) { + this.name = name; + this.age = age; +}; -// Principle 4 +const me = new Person('jeffrey', 24); + +/************************************************/ +// Principle 4 // code example for Explicit Binding +const heyThere = function() { + console.log('hey, ' + this.name); +} +const me = { + name: 'jeffrey', + age: 24 +}; +heyThere.call(me); \ No newline at end of file