From 474d48b05e405ebe79b7829a4038f7971d6e76bc Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Thu, 5 Oct 2017 14:04:25 -0700 Subject: [PATCH 1/6] class.js passed --- src/class.js | 31 ++++++++++++++++++++++++++++++- src/recursion.js | 13 +++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/class.js b/src/class.js index e7a0a2b..1334924 100644 --- a/src/class.js +++ b/src/class.js @@ -8,7 +8,16 @@ // Return true if the potential password matches the `password` property. Otherwise return false. // code here - +class User { + // Make sure options is first parameter + constructor(options, email, password) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(checkPass) { + return this.password === checkPass; + } +} // Part 2 // Create a class called `Animal` and a class called `Cat` using ES6 classes. // `Cat` should extend the `Animal` class. @@ -20,6 +29,26 @@ // property set on the Cat instance. // code here +class Animal { + constructor(options, age) { + this.age = options.age; + } + growOlder() { + return ++this.age; + } +} + +class Cat extends Animal { + constructor(options, name) { + super(options); + this.name = name; + } + meow() { + const name = this.name; + const newString = ' meowed!'; + return newString; + } +} /* eslint-disable no-undef */ diff --git a/src/recursion.js b/src/recursion.js index 117db24..1669451 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,11 +3,24 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence + // if n = 3 then it is 1 + 2; + // if n = 4 then it is 2 + 3 returning 5; + if (n === 1 || n === 2) { + return n; + } + else { + + } + return 2 + nFibonacci(n - 1); }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + if (n === 1) { + return 1; + } + return n * nFactorial(n - 1); }; /* Extra Credit */ From 3a4bb6a016480de2952514c0411d010b64abc230 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Thu, 5 Oct 2017 17:26:30 -0700 Subject: [PATCH 2/6] prototype.js test completed --- src/class.js | 7 +++---- src/prototype.js | 33 +++++++++++++++++++++++++++++++++ src/recursion.js | 11 ++--------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/class.js b/src/class.js index 1334924..c6d9f78 100644 --- a/src/class.js +++ b/src/class.js @@ -10,7 +10,7 @@ // code here class User { // Make sure options is first parameter - constructor(options, email, password) { + constructor(options) { this.email = options.email; this.password = options.password; } @@ -34,6 +34,7 @@ class Animal { this.age = options.age; } growOlder() { + // increment age return ++this.age; } } @@ -44,9 +45,7 @@ class Cat extends Animal { this.name = name; } meow() { - const name = this.name; - const newString = ' meowed!'; - return newString; + return this.name.concat(' meowed!'); } } diff --git a/src/prototype.js b/src/prototype.js index e2494a6..27eef2b 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -49,6 +49,39 @@ hamsterHuey.destroy(); // returns 'Game object was removed from the game.' */ +function GameObject(options) { + this.createdAt = options.createdAt; + this.dimensions = options.dimensions; +} + +GameObject.prototype.destroy = function () { + return 'Game object was removed from the game.'; +}; + +function NPC(options, createdAt, dimensions, hp, name) { + GameObject.call(this, options, createdAt, dimensions); + this.hp = options.hp; + this.name = options.name; +} + +NPC.prototype = Object.create(GameObject.prototype); + +NPC.prototype.takeDamage = function () { + return this.name.concat(' took damage.'); +}; + +function Humanoid(options, createdAt, dimensions, hp, name, faction, weapon, language) { + NPC.call(this, options, createdAt, dimensions, hp, name); + this.faction = options.faction; + this.weapons = options.weapons; + this.language = options.language; +} + +Humanoid.prototype = Object.create(NPC.prototype); +Humanoid.prototype.greet = function () { + return this.name.concat(' offers a greeting in ').concat(this.language).concat('.'); +}; + /* eslint-disable no-undef */ module.exports = { diff --git a/src/recursion.js b/src/recursion.js index 1669451..d675db0 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,15 +3,8 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence - // if n = 3 then it is 1 + 2; - // if n = 4 then it is 2 + 3 returning 5; - if (n === 1 || n === 2) { - return n; - } - else { - - } - return 2 + nFibonacci(n - 1); + // if n = 3 then it is 1 + 2; + // if n = 4 then it is 2 + 3 returning 5; }; const nFactorial = (n) => { From 547254912892d5853bbac84bf93c0b2ce92737a9 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Fri, 6 Oct 2017 10:03:21 -0700 Subject: [PATCH 3/6] Javascript-II Recursion Commit --- src/recursion.js | 4 ++++ src/this.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index d675db0..fcd1e06 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -5,6 +5,10 @@ const nFibonacci = (n) => { // return the nth number in the sequence // if n = 3 then it is 1 + 2; // if n = 4 then it is 2 + 3 returning 5; + if (n <= 2) { + return n; + } + return nFibonacci(n - 1) + nFibonacci(n - 2); }; const nFactorial = (n) => { diff --git a/src/this.js b/src/this.js index f0f994c..f0dcecd 100644 --- a/src/this.js +++ b/src/this.js @@ -7,10 +7,18 @@ 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; } // 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 // return `true` if they match, otherwise return `false` + checkPassword(str) { + if (this.password === str) { + return true; + } + return false; + } } const me = new User({ From 48d460684ff088979076621c84e2858f669151b4 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Fri, 6 Oct 2017 14:34:27 -0700 Subject: [PATCH 4/6] JavaScript-II Hw updated --- src/recursion.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index fcd1e06..a1e40ac 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -24,6 +24,15 @@ const nFactorial = (n) => { const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + + let objProperty = false; + for (let i = 0; i < obj.length; i++) { + if (Array.isArray(obj[i])) { + checkMatchingLeaves(obj[i]); + objProperty = true; + } + } + return objProperty; }; /* eslint-enable no-unused-vars */ From b3cabcdaa1649288c50b3ce9a1e5828e217b2965 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Fri, 6 Oct 2017 16:28:01 -0700 Subject: [PATCH 5/6] JavaSCript-II this.js commit --- src/this.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/this.js b/src/this.js index f0dcecd..6f6a673 100644 --- a/src/this.js +++ b/src/this.js @@ -35,13 +35,19 @@ 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 `=>` + + if (me.password === passwordToCompare) { + return true; + } + return false; }; // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind // .call - +checkPassword.call(me); // .apply - +checkPassword.apply(me); // .bind +const boundPassword = checkPassword.bind(me); From 627bf1813b57815e46770e0c9f725b9b8ad1ae57 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Mon, 9 Oct 2017 12:59:24 -0700 Subject: [PATCH 6/6] updated JavaScript-II hw --- src/recursion.js | 28 ++++++++++++++++++++-------- src/this.js | 11 +++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index a1e40ac..13a87e7 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -24,15 +24,27 @@ const nFactorial = (n) => { const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + let val; + let allMatch = true; + const checkLeaves = (object) => { + Object.keys(object).forEach((key) => { + if (val === undefined && typeof key !== 'object') { + val = object[key]; + return; + } - let objProperty = false; - for (let i = 0; i < obj.length; i++) { - if (Array.isArray(obj[i])) { - checkMatchingLeaves(obj[i]); - objProperty = true; - } - } - return objProperty; + if (typeof object[key] === 'object') { + return checkLeaves(object[key]); + } + if (object[key !== val]) { + allMatch = false; + return; + } + return; + }); + }; + checkLeaves(obj); + return allMatch; }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index 6f6a673..ae70cf2 100644 --- a/src/this.js +++ b/src/this.js @@ -36,18 +36,17 @@ const checkPassword = function comparePasswords(passwordToCompare) { // do not modify this function's parameters // note that we use the `function` keyword and not `=>` - if (me.password === passwordToCompare) { - return true; - } - return false; + return this.password === passwordToCompare; }; // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind +const correctPassword = 'correcthorsebatterystaple'; // .call -checkPassword.call(me); +checkPassword.call(me, correctPassword); // .apply -checkPassword.apply(me); +checkPassword.apply(me, [correctPassword]); // .bind const boundPassword = checkPassword.bind(me); +boundPassword([correctPassword]);