Skip to content

Commit 94db91b

Browse files
committed
Complete recursion
1 parent 93e5e64 commit 94db91b

3 files changed

Lines changed: 168 additions & 18 deletions

File tree

‎package-lock.json‎

Lines changed: 148 additions & 17 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/recursion.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@
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+
if (n === 1) return n;
14+
return n *= nFactorial(n - 1);
1115
};
1216

1317
/* Extra Credit */
1418
const checkMatchingLeaves = (obj) => {
1519
// return true if every property on `obj` is the same
1620
// otherwise return false
21+
const allValues = [];
22+
23+
const checkInner = (innerObj) => {
24+
Object.values(innerObj).forEach((ele) => {
25+
if (typeof ele === 'object') {
26+
checkInner(ele);
27+
} else {
28+
allValues.push(ele);
29+
}
30+
});
31+
32+
return allValues.every(ele => ele === allValues[0]);
33+
};
34+
35+
return checkInner(obj);
1736
};
1837

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

0 commit comments

Comments
 (0)