Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
74 changes: 74 additions & 0 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
14 changes: 10 additions & 4 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,21 +24,22 @@ const me = new User({
});

const result = me.checkPassword('correcthorsebatterystaple'); // should return `true`

/* part 2 */

const checkPassword = function comparePasswords(passwordToCompare) {
// recreate the `checkPassword` method that you made on the `User` class
// 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');