diff --git a/src/class.js b/src/class.js index e7a0a2b..c6d9f78 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) { + 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,25 @@ // property set on the Cat instance. // code here +class Animal { + constructor(options, age) { + this.age = options.age; + } + growOlder() { + // increment age + return ++this.age; + } +} + +class Cat extends Animal { + constructor(options, name) { + super(options); + this.name = name; + } + meow() { + return this.name.concat(' meowed!'); + } +} /* eslint-disable no-undef */ 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 117db24..13a87e7 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,17 +3,48 @@ 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 <= 2) { + return n; + } + return nFibonacci(n - 1) + nFibonacci(n - 2); }; 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 */ 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; + } + + 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 f0f994c..ae70cf2 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({ @@ -27,13 +35,18 @@ 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 +const correctPassword = 'correcthorsebatterystaple'; // .call - +checkPassword.call(me, correctPassword); // .apply - +checkPassword.apply(me, [correctPassword]); // .bind +const boundPassword = checkPassword.bind(me); +boundPassword([correctPassword]);