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
50 changes: 48 additions & 2 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,34 @@
// 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
// Part 1:
// Old way just for practice.

function User(options) {
this.email = options.email;
this.password = options.password;
}

User.prototype.comparePasswords = function (newPassword) {
return(this.password === newPassword);
};

const user = new User('[email protected]', 'asdf')

const result = user.comparePasswords('qwer');
console.log(result)

// New way.

Class User {
constructor(options) {
this.email = options.email;
this.password = options.password;
}
comparePasswords(newPassword) {
return(this.password === newPassword);
}
}

// Part 2
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
Expand All @@ -19,7 +46,26 @@
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
// property set on the Cat instance.

// code here
// Part 2:

Class Animal {
constructor(options) {
this.age = options.age;
}
growOlder() {
return(this.age);
}
}

Class Cat extends Animal {
constructor(options) {
super(options);
this.name = options.name;
}
moew() {
return(`${this.name} meowed!`)
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 don't need to wrap this in parenthesis

}
}

/* eslint-disable no-undef */

Expand Down
56 changes: 54 additions & 2 deletions src/prototype.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment
you will be implementing several classes with their correct inheritance heirarchy.

you will be implementing several classes with their correct inheritance heirarchy
In this file you will be creating three classes:
GameObject
createdAt
Expand Down Expand Up @@ -56,3 +56,55 @@ module.exports = {
NPC,
Humanoid,
};

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

GameObject.prototype.destroy = function() {
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.takeDamage = function() {
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.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();
hamsterHuey.takeDamage();
hamsterHuey.destroy();
8 changes: 8 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
const nFibonacci = (n) => {
// fibonacci sequence: 1 2 3 5 8 13 ...
// return the nth number in the sequence
if (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 (n);
let answer = 1;
for (let i = 1; i <= n; i++) {
(answer *= i);
}
return answer;
};

/* Extra Credit */
Expand Down
11 changes: 10 additions & 1 deletion src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

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(check) {
return (this.password === check);
}
// set a username and password property on the user object that is created

// 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`
Expand All @@ -33,7 +39,10 @@ const checkPassword = function comparePasswords(passwordToCompare) {
// use .call, .apply, and .bind

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

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

// .bind
const check = checkPassword.bind(me);