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
31 changes: 31 additions & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
// 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(str) {
if (str === 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 +30,27 @@
// property set on the Cat instance.

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

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

const simba = new Cat({age: 2, name: 'Simba'});
console.log(simba.growOlder())

/* eslint-disable no-undef */

Expand Down
52 changes: 52 additions & 0 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,58 @@
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
*/

class GameObject {
constructor(options) {
this.createdAt = options.createdAt;
this.dimensions = options.dimensions;
}
destroy() {
return 'Game object was removed from the game';
}
}

class NPC extends GameObject {
constructor(options) {
super(options);
this.hp = options.hp;
this.name = options.name;
}
takeDamage() {
return `${this.name} took damage.`;
}
}

class Humanoid extends NPC {
constructor(options) {
super(options);
this.faction = options.faction;
this.weapons = options.weapons;
this.language = options.language;
}
greet() {
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(); // returns 'Hamster Huey offers a greeting in Hamsterish'
hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
/* eslint-disable no-undef */

module.exports = {
Expand Down
6 changes: 6 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
const nFibonacci = (n) => {
// fibonacci sequence: 1 1 2 3 5 8 13 ...
// return the nth number in the sequence
// n - 6 (6th number in sequence is 8)... (n-1) + (n-2)... 5th + 4th... 5 + 3 = 8
return (n < 2) ? n : nFibonacci(n - 2) + nFibonacci(n - 1);
};

const nFactorial = (n) => {
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
// return the factorial of `n`
let result = 1;
if (n === 1 || n === 0) return n;
result *= n * nFactorial(n - 1); // 5 * 4 * 3 * 2
return result;
};

/* Extra Credit */
Expand Down
12 changes: 12 additions & 0 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
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;
return false;
}
}

const me = new User({
Expand All @@ -27,13 +33,19 @@ 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

// .call
checkPassword.call(me, 'kjgjhjkhdf');

// .apply
checkPassword.apply(me, ['jgdflhfdghkjldfg']);

// .bind
const myCheckPassword = checkPassword.bind(me, 'correcthorsebatterystaple');
myCheckPassword();
Loading