Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 2ed3237

Browse files
All passing
1 parent da10689 commit 2ed3237

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

src/class.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
// for a potential password that will be compared to the `password` property.
88
// Return true if the potential password matches the `password` property. Otherwise return false.
99

10-
// code here
10+
class User {
11+
constructor(options) {
12+
this.email = options.email;
13+
this.password = options.password;
14+
}
15+
comparePasswords(passMatch) {
16+
return this.password === passMatch;
17+
}
18+
}
1119

1220
// Part 2
1321
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
@@ -19,8 +27,24 @@
1927
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
2028
// property set on the Cat instance.
2129

22-
// code here
30+
class Animal {
31+
constructor(options) {
32+
this.age = options.age;
33+
}
34+
growOlder() {
35+
return ++this.age;
36+
}
37+
}
2338

39+
class Cat extends Animal {
40+
constructor(options) {
41+
super(options);
42+
this.name = options.name;
43+
}
44+
meow() {
45+
return `${this.name} meowed!`;
46+
}
47+
}
2448
/* eslint-disable no-undef */
2549

2650
module.exports = {

src/prototype.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,57 @@
4949
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
5050
*/
5151

52+
/*
53+
GameObject
54+
createdAt
55+
dimensions
56+
destroy() // prototype method -> returns the string 'Game object was removed from the game.'
57+
*/
58+
function GameObject(options) {
59+
this.createdAt = options.createdAt;
60+
this.dimensions = options.dimensions;
61+
}
62+
GameObject.prototype.destroy = function () {
63+
return 'Game object was removed from the game.';
64+
};
65+
66+
/*
67+
NPC
68+
hp
69+
name
70+
takeDamage() // prototype method -> returns the string '<object name> took damage.'
71+
// should inherit destroy() from GameObject's prototype
72+
*/
73+
function NPC(options) {
74+
GameObject.call(this, options);
75+
this.hp = options.hp;
76+
this.name = options.name;
77+
}
78+
NPC.prototype = Object.create(GameObject.prototype);
79+
NPC.prototype.takeDamage = function () {
80+
return `${this.name} took damage.`;
81+
};
82+
83+
/*
84+
Humanoid
85+
faction
86+
weapons
87+
language
88+
greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
89+
// should inherit destroy() from GameObject through NPC
90+
// should inherit takeDamage() from NPC
91+
*/
92+
function Humanoid(options) {
93+
NPC.call(this, options);
94+
this.faction = options.faction;
95+
this.weapons = options.weapons;
96+
this.language = options.language;
97+
}
98+
Humanoid.prototype = Object.create(NPC.prototype);
99+
Humanoid.prototype.greet = function () {
100+
return `${this.name} offers a greeting in ${this.language}.`;
101+
};
102+
52103
/* eslint-disable no-undef */
53104

54105
module.exports = {

src/recursion.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,44 @@
33
const nFibonacci = (n) => {
44
// fibonacci sequence: 1 2 3 5 8 13 ...
55
// return the nth number in the sequence
6+
if (n === 0 || n === 1) return 1;
7+
8+
return nFibonacci(n - 1) + nFibonacci(n - 2);
69
};
710

11+
nFibonacci(5);
12+
813
const nFactorial = (n) => {
914
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1015
// return the factorial of `n`
16+
if (n === 1) return n;
17+
18+
return n * nFactorial(n - 1);
1119
};
1220

1321
/* Extra Credit */
1422
const checkMatchingLeaves = (obj) => {
1523
// return true if every property on `obj` is the same
1624
// otherwise return false
25+
const check = (objPass) => {
26+
const arr = [];
27+
Object.keys(objPass).forEach((key) => {
28+
if (typeof objPass[key] === 'object') {
29+
const flatObj = check(objPass[key]);
30+
Object.keys(flatObj).forEach((secKey) => {
31+
arr.push(flatObj[secKey]);
32+
});
33+
} else {
34+
arr.push(objPass[key]);
35+
}
36+
});
37+
return arr;
38+
};
39+
const arr = check(obj);
40+
for (let i = 0; i < arr.length; i++) {
41+
if (arr[i] !== arr[i + 1] && i + 1 !== arr.length) return false;
42+
}
43+
return true;
1744
};
1845

1946
/* eslint-enable no-unused-vars */

src/this.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
class User {
88
constructor(options) {
99
// set a username and password property on the user object that is created
10+
this.username = options.username;
11+
this.password = options.password;
1012
}
1113
// create a method on the User class called `checkPassword`
1214
// this method should take in a string and compare it to the object's password property
1315
// return `true` if they match, otherwise return `false`
16+
checkPassword(matchPassword) {
17+
return matchPassword === this.password;
18+
}
1419
}
1520

1621
const me = new User({
@@ -27,6 +32,7 @@ const checkPassword = function comparePasswords(passwordToCompare) {
2732
// use `this` to access the object's `password` property.
2833
// do not modify this function's parameters
2934
// note that we use the `function` keyword and not `=>`
35+
return passwordToCompare === this.password;
3036
};
3137

3238
// invoke `checkPassword` on `me` by explicitly setting the `this` context
@@ -37,3 +43,4 @@ const checkPassword = function comparePasswords(passwordToCompare) {
3743
// .apply
3844

3945
// .bind
46+
checkPassword.call(me, 'correcthorsebatterystaple');

0 commit comments

Comments
 (0)