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..462b10e 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,17 +3,44 @@ 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 const nFibonacci = n => 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` + 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) => { // 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; + } + vals.push(newObj[prop]); + }); + }; + helper(obj); + 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');