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
127 changes: 80 additions & 47 deletions src/prototype.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,86 @@
/*
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.

In this file you will be creating three classes:
GameObject
createdAt
dimensions
destroy() // prototype method -> returns the string 'Game object was removed from the game.'

NPC
hp
name
takeDamage() // prototype method -> returns the string '<object name> took damage.'
// should inherit destroy() from GameObject's prototype

Humanoid
faction
weapons
language
greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
// 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.

// In this file you will be creating three classes:
// GameObject
// createdAt
// dimensions
// destroy() // prototype method -> returns the string 'Game object was removed from the game.'


class GameObject {
constructor(attributes) {
this.createdAt = attributes.createdAt;
this.dimensions = attributes.dimensions;
}

GameObject.prototype.destroy = function() {
return `Game object was removed from the game.`;
// NPC
// hp
// name
// takeDamage() // prototype method -> returns the string '<object name> took damage.'
// // should inherit destroy() from GameObject's prototype
function NPC(attributes) {
GameObject.call(this, attributes);
this.hp = attributes.hp;
this.name = attributes.name;
}

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

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

// Humanoid
// faction
// weapons
// language
// greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
// should inherit destroy() from GameObject through NPC
// should inherit takeDamage() from NPC

Inheritance chain: Humanoid -> NPC -> GameObject
Instances of Humanoid should have all of the same properties as NPC and GameObject.
Instances of NPC should have all of the same properties as GameObject.

Example:

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.'
*/
// Inheritance chain: Humanoid -> NPC -> GameObject
// Instances of Humanoid should have all of the same properties as NPC and GameObject.
// Instances of NPC should have all of the same properties as GameObject.

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

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

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


// Example:

// 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 */

Expand Down
7 changes: 7 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
const nFibonacci = (n) => {
// fibonacci sequence: 1 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 === 0) {
return 1;
}
return n * nFactorial(n - 1);
};

/* Extra Credit */
Expand Down
9 changes: 9 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(pass) {
return this.password === pass;
}
}

const me = new User({
Expand All @@ -27,13 +32,17 @@ 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
checkPassword.call(me, ['correcthorsebatterystaple']);

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

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