Skip to content
Open
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* prototype methods vs methods in the constructor (Methods that inherit via the prototype chain can be changed universally for all instances)
* class vs instance


## Instructions

* Fork and clone this repo.
Expand Down
34 changes: 31 additions & 3 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,47 @@
// for a potential password that will be compared to the `password` property.
// Return true if the potential password matches the `password` property. Otherwise return false.

// code here
class User {
constructor(options) {
this.email = options.email;
this.password = options.password;
}

comparePasswords(providedPassword) {
return this.password === providedPassword;
}
}

// Part 2
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
// `Cat` should extend the `Animal` class.
// Animal and Cat should both have a parameter called `options` in their constructors.
// Animal should have the property `age` that's set in the constructor and the method
// `growOlder` that returns the age.
// `growOlder` that returns the age after incrementing it.
// Cat should have the property `name` that is set in the constructor and the method
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
// property set on the Cat instance.

// code here
class Animal {
constructor(options) {
this.age = options.age;
}

growOlder() {
return this.age += 1;
}
}

class Cat extends Animal {
constructor(options) {
super(options);
this.name = options.name;
}

meow() {
return `${this.name} meowed!`;
}
}

/* eslint-disable no-undef */

Expand Down
36 changes: 36 additions & 0 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,42 @@

/* eslint-disable no-undef */

function GameObject(options) {
this.createdAt = options.createdAt;
this.dimensions = options.dimensions;
}

GameObject.prototype.destroy = function destroy() {
return 'Game object was removed from the game.';
};

function NPC(options) {
GameObject.call(this, options);
this.hp = options.hp;
this.name = options.name;
}

NPC.prototype = Object.create(GameObject.prototype);
NPC.prototype.constructor = NPC;

NPC.prototype.takeDamage = function takeDamage() {
return `${this.name} took damage.`;
};

function Humanoid(options) {
NPC.call(this, options);
this.faction = options.faction;
this.weapons = options.weapons;
this.language = options.language;
}

Humanoid.prototype = Object.create(NPC.prototype);
Humanoid.prototype.constructor = Humanoid;

Humanoid.prototype.greet = function greet() {
return `${this.name} offers a greeting in ${this.language}.`;
};

module.exports = {
GameObject,
NPC,
Expand Down
22 changes: 21 additions & 1 deletion src/recursion.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
// Complete the following functions.

const nFibonacci = (n) => {
// fibonacci sequence: 1 2 3 5 8 13 ...
// fibonacci sequence: 1 1 2 3 5 8 13 ...
// return the nth number in the sequence
if (n === 1 || n === 2) return 1;

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 getAllLeaves = (obj) => {
return Object.values(obj).reduce((leaves, leaf) => {
if (typeof leaf === 'object') {
return [...leaves, ...getAllLeaves(leaf)];
}

return [...leaves, leaf];
}, []);
};

const checkMatchingLeaves = (obj) => {
// return true if every property on `obj` is the same
// otherwise return false

const leaves = getAllLeaves(obj);
return getAllLeaves(obj).every(leaf => leaf === leaves[0]);
};

/* eslint-enable no-unused-vars */
Expand Down
11 changes: 11 additions & 0 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
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(suppliedPassword) {
return suppliedPassword === this.password;
}
}

const me = new User({
Expand All @@ -19,6 +24,7 @@ const me = new User({
});

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

/* part 2 */

Expand All @@ -27,13 +33,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

// .call
console.log(checkPassword.apply(me, ['correcthorsebatterystaple']));

// .apply
console.log(checkPassword.call(me, 'correcthorsebatterystaple'));

// .bind
const bindCheckPassword = checkPassword.bind(me);
console.log(bindCheckPassword('correcthorsebatterystaple'));
4 changes: 2 additions & 2 deletions tests/recursion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('recursion', () => {
const fib1 = recursiveMethods.nFibonacci(5);
const fib2 = recursiveMethods.nFibonacci(3);
const fib3 = recursiveMethods.nFibonacci(1);
expect(fib1).toBe(8);
expect(fib2).toBe(3);
expect(fib1).toBe(5);
expect(fib2).toBe(2);
expect(fib3).toBe(1);
});
});
Expand Down