-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion.js
78 lines (39 loc) · 1.17 KB
/
recursion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*Recursion - a process that calls itself */
//Two Parts of Recursive Function - Base case and Different Input
//Factorial Iteratively
// function factorial(num) {
// let total = 1
// for(let i = num; i > 0; i--) {
// total *= i
// }
// return total
// }
// console.log(factorial(3))
//Factorial Recursive
function factorial(num) {
if(num === 1) return 1 //base case
return num * factorial(num - 1)
}
console.log(factorial(5))
//Helper Method Recursion
function collectOddsValues(arr) {
let result = []
function helper(helperInput) {
if(helperInput.length === 0) {
return
}
if(helperInput[0] % 2 !== 0) {
result.push(helperInput[0])
}
helper(helperInput.slice(1))
}
helper(arr)
return result
}
console.log(collectOddsValues([1,2,3,4,5,6,7,8,9]))
//Pure Recursion Tips
/*
- For arrays, use methods like slice, the spread operator and concat that copies of arrays so you dont mutate them
- strings are immutable so you will need to use methods like slice, substr
- To make copies of objects use Object.assign or the spread operator
*/