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
30 changes: 30 additions & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
// 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(pw) {
if (pw === this.password) return true;
return false;
}
}


// Part 2
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
Expand All @@ -20,6 +31,25 @@
// property set on the Cat instance.

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


class Cat extends Animal {
constructor(options) {
super(options);
this.name = options.name;
}
meow(name) {
return `${this.name} meowed!`;
}
}

/* eslint-disable no-undef */

Expand Down
98 changes: 98 additions & 0 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,104 @@
hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
*/
function GameObject(object) {
this.createdAt = object.createdAt;
this.dimensions = object.dimensions;
// this.destroy = function() {
// return 'Game object was removed from the game.';
// };
}

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

function NPC(stats) {
GameObject.call(this, stats);
this.hp = stats.hp;
this.name = stats.name;
// this.takeDamage = function() {
// return `${this.name} took damage!`
// }
}

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

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

function Humanoid(bgInfo) {
NPC.call(this, bgInfo);
this.faction = bgInfo.faction;
this.weapons = bgInfo.weapons;
this.language = bgInfo.language;
this.greet = () => {
return `${this.name} offers a greeting in ${this.language}.`;
};
}

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

const mcgee = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
hp: 50,
name: 'McGeeicus Maximus',
faction: 'Wolf Riders',
weapons: [
'Wolf\'s Head Gladius',
],
language: 'Canine',
});

const aysah = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
hp: 40,
name: 'Aysah the Beautiful',
faction: 'Pretty Heart Princesses',
weapons: [
'Cupid\'s Bow',
],
language: 'Angelic',
});

const lilbear = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 2,
},
hp: 10,
name: 'Henry Bear',
faction: 'Omnivores',
weapons: [
'Clawed Cestus',
],
language: 'Ursine',
});

// console.log(mcgee.greet());
// console.log(mcgee.takeDamage());
// console.log(mcgee.destroy());

// console.log(aysah.greet());
// console.log(aysah.takeDamage());
// console.log(aysah.destroy());

// console.log(lilbear.greet());
// console.log(lilbear.takeDamage());
// console.log(lilbear.destroy());

/* eslint-disable no-undef */

Expand Down
26 changes: 24 additions & 2 deletions src/recursion.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
// Complete the following functions.

const nFibonacci = (n) => {
const nFibonacci = (n, tracker, num, prevNum) => {
// fibonacci sequence: 1 1 2 3 5 8 13 ...
// return the nth number in the sequence
// baseline
if (tracker === n) {
return num;
}
if (num === 0 && prevNum === 0) {
// console.log(++num);
// console.log(++prevNum);
tracker += 2;
return nFibonacci(n, tracker, num, prevNum);
}
const i = num;
num += prevNum;
prevNum = i;
// console.log (num);
return nFibonacci(n, ++tracker, num, prevNum);
};
// console.log('The fifth sequence in Fibonacci is ' + nFibonacci(5, 0, 0, 0));

const nFactorial = (n) => {
const nFactorial = (n, runningNum = 1, tracker = 2) => {
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
// return the factorial of `n`
if (tracker > n) {
return runningNum;
}
runningNum *= tracker;
return nFactorial(n, runningNum, ++tracker);
};
// console.log(nFactorial(6));

/* Extra Credit */
const checkMatchingLeaves = (obj) => {
Expand Down
16 changes: 14 additions & 2 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
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;
this.checkPassword = function (pw) {
if (pw === this.password) return true;
return false;
};
}
// 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,6 +25,7 @@ const me = new User({
});

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

/* part 2 */

Expand All @@ -27,13 +34,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 `=>`
if (this.password === passwordToCompare) return true;
return false;
};

// invoke `checkPassword` on `me` by explicitly setting the `this` context
// use .call, .apply, and .bind
const enteredPassword = ['correcthorsebatterystaple'];

// .call

// console.log(checkPassword.call (me, enteredPassword[0]));
// .apply

// console.log(checkPassword.apply (me, enteredPassword));
// .bind
const alsoCheckPW = checkPassword.bind(me.checkPassword);
// console.log (alsoCheckPW());
Loading