From bb8b30f5db95c6c0e327256fdc0b57449d5527c8 Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Thu, 5 Oct 2017 17:04:04 -0400 Subject: [PATCH 1/6] finished --- src/class.js | 29 +++++++++++++++++++ src/prototype.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/recursion.js | 1 + 3 files changed, 104 insertions(+) diff --git a/src/class.js b/src/class.js index e7a0a2b..c885741 100644 --- a/src/class.js +++ b/src/class.js @@ -23,6 +23,35 @@ /* eslint-disable no-undef */ +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(potentialPassword) { + return this.password === potentialPassword; + } +} + +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { + return this.age += 1; + } +} + +class Cat extends Animal { + constructor(options, name) { + super(options); + this.name = name; + } + meow() { + return `${this.name} meowed!`; + } +} + module.exports = { User, Cat, diff --git a/src/prototype.js b/src/prototype.js index e2494a6..2ffba08 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -51,6 +51,80 @@ /* eslint-disable no-undef */ +function GameObject(gameObjProps) { + this.createdAt = gameObjProps.createdAt; + this.dimensions = gameObjProps.dimensions; +} + +GameObject.prototype.destroy = function () { + return 'Game object was removed from the game.'; +}; + +const someGameObj = new GameObject({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, +}); + +function NPC(gameObjProps) { + GameObject.call(this, gameObjProps); + this.hp = gameObjProps.hp; + this.name = gameObjProps.name; +} + +NPC.prototype = Object.create(GameObject.prototype); + +NPC.prototype.takeDamage = function () { + return `${this.name} took damage.`; +}; + +const someNpcObj = new NPC({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + hp: 5, + name: 'Hamster Huey', +}); + +someNpcObj.takeDamage(); + +function Humanoid(gameObjProps) { + NPC.call(this, gameObjProps); + this.faction = gameObjProps.faction; + this.weapons = gameObjProps.weapons; + this.language = gameObjProps.language; +} + +Humanoid.prototype = Object.create(NPC.prototype); + +Humanoid.prototype.greet = function () { + return `${this.name} offers a greeting in ${this.language}.`; +}; + +const hamsterHuey = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + hp: 5, + name: 'Hamster Huey', + faction: 'Gooey Kablooie', + weapons: [ + 'bubblegum', + ], + language: 'Hamsterish', +}); + +hamsterHuey.greet(); + module.exports = { GameObject, NPC, diff --git a/src/recursion.js b/src/recursion.js index 117db24..224dece 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,6 +3,7 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence + return n; }; const nFactorial = (n) => { From 78152ee8d1d737e451171410ee6bb66e3883864e Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Thu, 5 Oct 2017 19:17:42 -0400 Subject: [PATCH 2/6] homework 2 --- src/recursion.js | 27 ++++++++++++++++++++++++++- src/this.js | 14 ++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 224dece..e4a7719 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,18 +3,43 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence - return n; + let result = 1; + let prevNum = 1; + const helper = (num) => { + if (n === 0) return; + prevNum = result; + result = num; + n -= 1; + return helper(result + prevNum); + }; + helper(result); + return result; }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + return n === 0 ? 1 : nFactorial(n - 1) * n; }; /* Extra Credit */ const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + const vals = []; + const helper = (newObj) => { + const props = Object.keys(newObj); + props.forEach((prop) => { + if (typeof newObj[prop] === 'object') { + helper(newObj[prop]); + return; + } + vals.push(newObj[prop]); + }); + }; + helper(obj); + console.log(vals); + return vals.every(val => val === vals[0]); }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index f0f994c..ac05c8e 100644 --- a/src/this.js +++ b/src/this.js @@ -7,6 +7,11 @@ class User { constructor(options) { // set a username and password property on the user object that is created + this.username = options.username; + this.password = options.password; + } + checkPassword(passwordToCompare) { + return this.password === passwordToCompare; } // create a method on the User class called `checkPassword` // this method should take in a string and compare it to the object's password property @@ -19,7 +24,6 @@ const me = new User({ }); const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` - /* part 2 */ const checkPassword = function comparePasswords(passwordToCompare) { @@ -27,13 +31,15 @@ const checkPassword = function comparePasswords(passwordToCompare) { // use `this` to access the object's `password` property. // do not modify this function's parameters // note that we use the `function` keyword and not `=>` + return this.password === passwordToCompare; }; // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind - // .call - // .apply - // .bind +checkPassword.call(me, 'correcthorsebatterystaple'); +checkPassword.apply(me, ['correcthorsebatterystaple']); +const boundCheckPassword = checkPassword.bind(me); +boundCheckPassword('correcthorsebatterystaple'); From cceac38faa8df6952b66350bd32c9fd544043267 Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Fri, 6 Oct 2017 16:58:05 -0400 Subject: [PATCH 3/6] added another solution --- src/recursion.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index e4a7719..010be64 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,21 +1,27 @@ // Complete the following functions. +// const nFibonacci = (n) => { +// // fibonacci sequence: 1 2 3 5 8 13 ... +// // return the nth number in the sequence +// let result = 1; +// let prevNum = 1; +// const helper = (num) => { +// if (n === 0) return; +// prevNum = result; +// result = num; +// n -= 1; +// return helper(result + prevNum); +// }; +// helper(result); +// return result; +// }; + +// or if I were math genius const nFibonacci = (n) => { - // fibonacci sequence: 1 2 3 5 8 13 ... - // return the nth number in the sequence - let result = 1; - let prevNum = 1; - const helper = (num) => { - if (n === 0) return; - prevNum = result; - result = num; - n -= 1; - return helper(result + prevNum); - }; - helper(result); - return result; + return n < 2 ? 1 : nFibonacci(n - 2) + nFibonacci(n - 1); }; + const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` @@ -38,7 +44,6 @@ const checkMatchingLeaves = (obj) => { }); }; helper(obj); - console.log(vals); return vals.every(val => val === vals[0]); }; From 94340b862793f121df5025ed5e7caa2f4220982d Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Sat, 7 Oct 2017 20:46:12 -0400 Subject: [PATCH 4/6] fixed condition --- src/recursion.js | 2 +- src/this.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/recursion.js b/src/recursion.js index 010be64..f762f08 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -36,7 +36,7 @@ const checkMatchingLeaves = (obj) => { const helper = (newObj) => { const props = Object.keys(newObj); props.forEach((prop) => { - if (typeof newObj[prop] === 'object') { + if (typeof newObj[prop] === 'object' && newObj[prop] !== null) { helper(newObj[prop]); return; } diff --git a/src/this.js b/src/this.js index ac05c8e..d5b1479 100644 --- a/src/this.js +++ b/src/this.js @@ -43,3 +43,4 @@ checkPassword.call(me, 'correcthorsebatterystaple'); checkPassword.apply(me, ['correcthorsebatterystaple']); const boundCheckPassword = checkPassword.bind(me); boundCheckPassword('correcthorsebatterystaple'); +console.log(result); \ No newline at end of file From 429ea4ef888b793475f21c5bc38266bfdc347a8a Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Sat, 7 Oct 2017 20:52:54 -0400 Subject: [PATCH 5/6] finished --- src/recursion.js | 32 ++++++++++++++------------------ src/this.js | 1 - 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index f762f08..f50791d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,26 +1,22 @@ // Complete the following functions. -// const nFibonacci = (n) => { -// // fibonacci sequence: 1 2 3 5 8 13 ... -// // return the nth number in the sequence -// let result = 1; -// let prevNum = 1; -// const helper = (num) => { -// if (n === 0) return; -// prevNum = result; -// result = num; -// n -= 1; -// return helper(result + prevNum); -// }; -// helper(result); -// return result; -// }; - -// or if I were math genius const nFibonacci = (n) => { - return n < 2 ? 1 : nFibonacci(n - 2) + nFibonacci(n - 1); + // fibonacci sequence: 1 2 3 5 8 13 ... + // return the nth number in the sequence + let result = 1; + let prevNum = 1; + const helper = (num) => { + if (n === 0) return; + prevNum = result; + result = num; + n -= 1; + return helper(result + prevNum); + }; + helper(result); + return result; }; +// or const nFibonacci = n => n < 2 ? 1 : nFibonacci(n - 2) + nFibonacci(n - 1); const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 diff --git a/src/this.js b/src/this.js index d5b1479..ac05c8e 100644 --- a/src/this.js +++ b/src/this.js @@ -43,4 +43,3 @@ checkPassword.call(me, 'correcthorsebatterystaple'); checkPassword.apply(me, ['correcthorsebatterystaple']); const boundCheckPassword = checkPassword.bind(me); boundCheckPassword('correcthorsebatterystaple'); -console.log(result); \ No newline at end of file From 7736bcd9257d484c953cd3f67f2e02117ffe84a1 Mon Sep 17 00:00:00 2001 From: dan volosnikov Date: Sun, 8 Oct 2017 14:14:07 -0400 Subject: [PATCH 6/6] finished --- src/recursion.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index f50791d..462b10e 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -31,8 +31,8 @@ const checkMatchingLeaves = (obj) => { const vals = []; const helper = (newObj) => { const props = Object.keys(newObj); - props.forEach((prop) => { - if (typeof newObj[prop] === 'object' && newObj[prop] !== null) { + props.forEach((prop) => { // or !!newObj[prop] && newObj[prop].constructor === Object or Object(newObj[prop]) === newObj[prop] + if (typeof newObj[prop] === 'object' && newObj[prop] !== null && !Array.isArray(newObj[prop])) { helper(newObj[prop]); return; }