From 6d6a3b074e07a1cd1811a3f5df1579d8705e3c30 Mon Sep 17 00:00:00 2001 From: Siddartha08 Date: Sun, 14 Jan 2018 15:51:37 -0600 Subject: [PATCH 1/2] JP Emery: Javascript Mini II recursion only --- recursion.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/recursion.js b/recursion.js index cb0d56e..fc44f17 100644 --- a/recursion.js +++ b/recursion.js @@ -9,11 +9,17 @@ while (n <= 10) { } // write a recursive - function called countToTen that mimics the while loop above. +let num = 1; +const countToTen = (num) => { + if(num > 10) return; + console.log(num); + countToTen(num + 1); + +} -// code here // when you code is ready, un-comment the next line and run the file -// console.log(countToTen()); +console.log(countToTen(1)); /* ================ Next Problem ================= */ // Problem 2: @@ -29,6 +35,15 @@ const factorial = n => { console.log(factorial(5)); // write the above functionin a recursive way. +//n * n - 1 +const recursiveFactorial = (num) => { + //base case: num === 0 return 1 + if (num === 0) return 1; + + return num * recursiveFactorial(num - 1); +} + + // when you code is ready, un-comment the next line and run the file -// console.log(recursiveFactorial()); +console.log(recursiveFactorial(5)); From 77ab84322901568a96eb627c97cc4cddbaddc23e Mon Sep 17 00:00:00 2001 From: Siddartha08 Date: Wed, 17 Jan 2018 20:00:20 -0600 Subject: [PATCH 2/2] JP Emery: commit updated from previous class --- constructors.js | 19 ++++++++++++++----- package.json | 19 +++++++++++++++++++ this.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 package.json diff --git a/constructors.js b/constructors.js index d0c9de2..ba71e6b 100644 --- a/constructors.js +++ b/constructors.js @@ -8,6 +8,9 @@ function Animal(options) { this.name = options.name; } +Animal.prototype.grow = function(name){ + console.log(`${this.name} grew larger!`); +} // add 'grow' to Animal's prototype here // problem #2 @@ -15,19 +18,25 @@ function Animal(options) { // the Animal constructor needs to be invoked with the 'options' argument // Cat should have its prototype inherit from Animal // instances of Cat should also have access to the 'grow' method +Cat.prototype = Object.create(Animal.prototype); + function Cat(options) { + Animal.call(this, options); + // invoke Animal here with .call } + + // 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/package.json b/package.json new file mode 100644 index 0000000..3cdae6b --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "help", + "version": "1.0.0", + "description": "* Recursion\r * the `this` keyword\r * constructor functions\r * classes\r ### Instructions\r * This repository is provided as more of a follow along structure.", + "main": "classes.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Siddartha08/JavaScript-II-Mini.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Siddartha08/JavaScript-II-Mini/issues" + }, + "homepage": "https://github.com/Siddartha08/JavaScript-II-Mini#readme" +} diff --git a/this.js b/this.js index 895b5fe..1c2cb07 100644 --- a/this.js +++ b/this.js @@ -13,16 +13,49 @@ console.log('hello world!'); // Principle 1 +function whatIsThis(){ + consol.log(this); +} +whatIsThis(); + // code example for Window Binding // Principle 2 +let newObj = { + name: 'name', + whatIsThis: function(){ + console.log(this); + } +}; +newObj.whatIsThis(); + // code example for Implicit Binding // Principle 3 +function User(name, pass, email){ + this.username = name; + this.password = pass; + this.email = email; + this.getsPass = function(){ + console.log(`${this.username}, ${this.password}`); + } +} + +let jp = new User('jp', '123456', 'jp@jpemery.com'); +let alex = new User('alex', '456123', 'alex@alex.com') // code example for New Binding // Principle 4 +jp.getsPass.bind(alex); + + +jp.getsPass.call(alex); + +let arr = ['secret', 'anotherSecret'] + +jp.getsPass.apply(alex, arr); + // code example for Explicit Binding