Skip to content

Commit 356641e

Browse files
committed
Finished working on recursion
1 parent 6f07d63 commit 356641e

File tree

3 files changed

+70
-56
lines changed

3 files changed

+70
-56
lines changed

src/class.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ class User {
1414
this.password = options.password;
1515
}
1616

17-
comparePasswords () {
18-
if (options.password = this.password)
19-
return true;
20-
}
17+
// comparePasswords() {
18+
// if(options.password = this.password) return true;
19+
// }
2120

22-
}
23-
const checkPasswd = new User({email:'[email protected]', password:'123456789'});
24-
console.log(checkPasswd);
25-
checkPasswd.comparePasswords('12345600');
2621

22+
// let checkPasswd = new User({ email: 'adfaris@gmail.com', password: '123456789' });
23+
// return (checkPasswd);
24+
// checkPasswd.comparePasswords('12345600');
25+
}
2726
// Part 2
2827
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
2928
// `Cat` should extend the `Animal` class.
@@ -34,7 +33,7 @@ checkPasswd.comparePasswords('12345600');
3433
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
3534
// property set on the Cat instance.
3635

37-
//code here
36+
// code here
3837

3938
// class Animal {
4039
// constructor(obj){

src/prototype.js

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66
GameObject
77
createdAt
88
dimensions
9-
destroy() // prototype method -> returns the string 'Game object was removed from the game.'*/
9+
destroy() // prototype method -> returns the string 'Game object was removed from the game.' */
1010

11-
function GameObject (gameobj) {
12-
this.createdAt = gameobj.createdAt;
13-
this.dimensions = gameobj.dimensions;
14-
}
11+
function GameObject(gameobj) {
12+
this.createdAt = gameobj.createdAt;
13+
this.dimensions = gameobj.dimensions;
14+
}
1515

16-
GameObject.prototype.distroy = function(){
17-
console.log('Game object was removed from the game');
18-
}
16+
GameObject.prototype.distroy = () => {
17+
return 'Game object was removed from the game';
18+
};
1919
/*
2020
NPC
2121
hp
2222
name
2323
takeDamage() // prototype method -> returns the string '<object name> took damage.'
2424
// should inherit destroy() from GameObject's prototype
2525
*/
26-
function NPC(npcObj){
27-
GameObject.call(this,npcObj);
28-
this. hp = npcObj.hp;
29-
this.name = npcObj.name;
30-
}
26+
function NPC(npcObj) {
27+
GameObject.call(this, npcObj);
28+
this.hp = npcObj.hp;
29+
this.name = npcObj.name;
30+
}
3131

32-
NPC.prototype.takeDamage = function(){
33-
console.log(`${this.name} took damage`);
34-
}
32+
NPC.prototype.takeDamage = () => {
33+
return `${this.name} took damage`;
34+
};
3535
/*
3636
Humanoid
3737
faction
@@ -41,44 +41,36 @@
4141
// should inherit destroy() from GameObject through NPC
4242
// should inherit takeDamage() from NPC
4343
*/
44-
function Humanoid (humanoidObj){
45-
GameObject.call(this.humanoidObj);
46-
this.faction = humanoidObj.faction;
47-
this.weapons = humanoidObj.weapons;
48-
this.language = humanoidObj.language
49-
}
44+
function Humanoid(humanoidObj) {
45+
GameObject.call(this.humanoidObj);
46+
this.faction = humanoidObj.faction;
47+
this.weapons = humanoidObj.weapons;
48+
this.language = humanoidObj.language;
49+
}
5050

51-
Humanoid.prototype.greet = function(){
52-
console.log(`${this.name} offers greetings in ${this.language}`);
53-
}
51+
Humanoid.prototype.greet = () => {
52+
return `${this.name} offers greetings in ${this.language}`;
53+
};
5454
/*
5555
Inheritance chain: Humanoid -> NPC -> GameObject
5656
Instances of Humanoid should have all of the same properties as NPC and GameObject.
5757
Instances of NPC should have all of the same properties as GameObject.
5858
5959
Example:
6060
*/
61-
const hamsterHuey = new Humanoid({
62-
createdAt: new Date(),
63-
dimensions: {
64-
length: 2,
65-
width: 1,
66-
height: 1,
67-
},
68-
hp: 5,
69-
name: 'Hamster Huey',
70-
faction: 'Gooey Kablooie',
71-
weapons: [
72-
'bubblegum',
73-
],
74-
language: 'Hamsterish',
75-
});
76-
77-
hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish'
78-
hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
79-
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
80-
61+
const hamsterHuey = new Humanoid({
62+
createdAt: new Date(),
63+
dimensions: 2,
64+
hp: 5,
65+
name: 'Hamster Huey',
66+
faction: 'Gooey Kablooie',
67+
weapons: ['bubblegum'],
68+
language: 'Hamsterish',
69+
});
8170

71+
hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish'
72+
hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
73+
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
8274

8375
/* eslint-disable no-undef */
8476

src/recursion.js

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

810
const nFactorial = (n) => {
911
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1012
// return the factorial of `n`
13+
// const nFactorial = n => {
14+
let result = 1;
15+
if (n === 0) return result;
16+
result = n * nFactorial(n - 1);
17+
return result;
1118
};
1219

1320
/* Extra Credit */
1421
const checkMatchingLeaves = (obj) => {
15-
// return true if every property on `obj` is the same
16-
// otherwise return false
22+
const arr = [];
23+
const recurse = (someObj) => {
24+
const keys = Object.keys(someObj);
25+
keys.forEach((item) => {
26+
if (typeof someObj[item] === 'object') {
27+
recurse(someObj[item]);
28+
} else {
29+
arr.push(someObj[item]);
30+
}
31+
});
32+
};
33+
recurse(obj);
34+
for (let i = 1; i < arr.length; i++) {
35+
if (arr[0] !== arr[i]) {
36+
return false;
37+
}
38+
}
39+
return true;
1740
};
1841

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

0 commit comments

Comments
 (0)