Skip to content

Commit 0d5e55e

Browse files
committed
completed recursion
1 parent b3e8a96 commit 0d5e55e

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/recursion.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
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 - 2) + nFibonacci(n - 1);
68
};
79

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

1319
/* Extra Credit */
1420
const checkMatchingLeaves = (obj) => {
1521
// return true if every property on `obj` is the same
1622
// otherwise return false
23+
const leaves = [];
24+
const findLeaves = (object) => {
25+
Object.values(object).forEach((value) => {
26+
if (value === Object(value)) {
27+
findLeaves(value);
28+
} else {
29+
leaves.push(value);
30+
}
31+
});
32+
};
33+
findLeaves(obj);
34+
return leaves.every(x => x === leaves[0]);
1735
};
1836

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

‎src/this.js‎

Lines changed: 7 additions & 1 deletion
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(pwd) {
17+
return pwd === this.password;
18+
}
1419
}
1520

1621
const me = new User({
@@ -27,11 +32,12 @@ 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+
3036
};
3137

3238
// invoke `checkPassword` on `me` by explicitly setting the `this` context
3339
// use .call, .apply, and .bind
34-
40+
me.checkPassword();
3541
// .call
3642

3743
// .apply

0 commit comments

Comments
 (0)