Skip to content

Commit 8b1a2e1

Browse files
committed
Prototype & Class both passing all
1 parent da10689 commit 8b1a2e1

5 files changed

Lines changed: 205 additions & 19 deletions

File tree

package-lock.json

Lines changed: 139 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"repository": {
1313
"type": "git",
1414
"url": "git+https://github.com/LambdaSchool/javascript-ii.git"
15-
},
15+
},
1616
"devDependencies": {
1717
"babel-jest": "^19.0.0",
1818
"eslint": "^3.17.1",

src/class.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
// Return true if the potential password matches the `password` property. Otherwise return false.
99

1010
// code here
11+
class User {
12+
constructor(options) {
13+
this.email = options.email;
14+
this.password = options.password;
15+
}
16+
comparePasswords(pw) {
17+
if (this.password === pw) {
18+
return true;
19+
} return false;
20+
}
21+
}
1122

1223
// Part 2
1324
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
@@ -20,7 +31,23 @@
2031
// property set on the Cat instance.
2132

2233
// code here
23-
34+
class Animal {
35+
constructor(options) {
36+
this.age = options.age;
37+
}
38+
growOlder(options) {
39+
return ++this.age;
40+
}
41+
}
42+
class Cat extends Animal {
43+
constructor(age, growOlder) {
44+
super(age, growOlder);
45+
this.name = Cat.name;
46+
}
47+
meow() {
48+
return `${this.name} meowed!`;
49+
}
50+
}
2451
/* eslint-disable no-undef */
2552

2653
module.exports = {

src/prototype.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,49 @@
66
GameObject
77
createdAt
88
dimensions
9-
destroy() // prototype method -> returns the string 'Game object was removed from the game.'
10-
11-
NPC
9+
destroy() // prototype method -> returns the string 'Game object was removed from the game.' */
10+
function GameObject(stats) {
11+
this.createdAt = new Date();
12+
this.dimensions = stats.dimensions;
13+
}
14+
GameObject.prototype.destroy = function () {
15+
return 'Game object was removed from the game.';
16+
}; // in this notation format, the function must be created outside of the object
17+
// strings not incorporating methods from the Object must use '', not ``
18+
/* NPC
1219
hp
1320
name
1421
takeDamage() // prototype method -> returns the string '<object name> took damage.'
15-
// should inherit destroy() from GameObject's prototype
16-
17-
Humanoid
22+
// should inherit destroy() from GameObject's prototype */
23+
function NPC(stats) {
24+
GameObject.call(this, stats);
25+
this.hp = stats.hp;
26+
this.name = stats.name;
27+
}
28+
NPC.prototype = Object.create(GameObject.prototype);
29+
// ^^ you must complete the prototype chain here in order for it to work see p2 of your notes, the
30+
// example item doesn't stop at the first page
31+
NPC.prototype.takeDamage = function () {
32+
return `${this.name} took damage.`; // this line can use `` because it's calling a method
33+
};
34+
/* Humanoid
1835
faction
1936
weapons
2037
language
2138
greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
2239
// should inherit destroy() from GameObject through NPC
23-
// should inherit takeDamage() from NPC
24-
25-
Inheritance chain: Humanoid -> NPC -> GameObject
40+
// should inherit takeDamage() from NPC */
41+
function Humanoid(stats) {
42+
NPC.call(this, stats);
43+
this.faction = stats.faction;
44+
this.weapons = stats.weapons;
45+
this.language = stats.language;
46+
}
47+
Humanoid.prototype = Object.create(NPC.prototype); // don't forget to finish the prototype chain...
48+
Humanoid.prototype.greet = function () {
49+
return `${this.name} offers a greeting in ${this.language}.`;
50+
};
51+
/* Inheritance chain: Humanoid -> NPC -> GameObject
2652
Instances of Humanoid should have all of the same properties as NPC and GameObject.
2753
Instances of NPC should have all of the same properties as GameObject.
2854

src/recursion.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const nFibonacci = (n) => {
88
const nFactorial = (n) => {
99
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1010
// return the factorial of `n`
11+
if (n === 1) return 1;
12+
return n * nFactorial(n - 1);
1113
};
1214

1315
/* Extra Credit */

0 commit comments

Comments
 (0)