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
30 changes: 29 additions & 1 deletion src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 */

Expand Down
33 changes: 33 additions & 0 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
31 changes: 31 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
17 changes: 15 additions & 2 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do this with one line return this.password === str

}
return false;
}
}

const me = new User({
Expand All @@ -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]);