You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 30, 2019. It is now read-only.
Copy file name to clipboardExpand all lines: src/recursion.js
+21-2Lines changed: 21 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,35 @@
3
3
constnFibonacci=(n)=>{
4
4
// fibonacci sequence: 1 2 3 5 8 13 ...
5
5
// return the nth number in the sequence
6
+
if(n<=1)return1;
7
+
returnnFibonacci(n-2)+nFibonacci(n-1);
6
8
};
7
9
8
10
constnFactorial=(n)=>{
9
11
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
10
12
// return the factorial of `n`
13
+
if(n<=1)return1;
14
+
returnn*nFactorial(n-1);
11
15
};
12
16
13
17
constcheckMatchingLeaves=(obj)=>{
14
-
// return true if every property on `obj` is the same
15
-
// otherwise return false
18
+
// return true if every property on `obj` is the same.
19
+
// otherwise return false.
20
+
constobs=[obj];// Set 'obs' to equal a 2d array of the object.
21
+
letcheck;
22
+
letflag=true;
23
+
consthelper=(o)=>{
24
+
constvals=Object.values(o);
25
+
for(leti=0;i<vals.length;i++){// Loop through values in object to check if they're all the same.
26
+
if(typeofvals[i]!=='object'&&!check)check=vals[i];// Set check equal to the first value in the object.
27
+
if(typeofvals[i]!=='object'&&check!==vals[i])returnflag=false;// If the current value is not an object AND check is not the same as the current value, return 'flag' as false.
28
+
if(typeofvals[i]==='object')obs.push(vals[i]);// If the current value IS an object, add the current value to the 'obs' object.
29
+
}
30
+
};
31
+
for(leti=0;i<obs.length;i++){// Loop through the 'obs' array.
32
+
helper(obs[i]);// For each value in the 'obs' array pass it to the helper function.
33
+
}
34
+
returnflag;// Return the state of flag, which will only be false if specifically set false.
0 commit comments