-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.js
84 lines (73 loc) · 1.25 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//counter example
/*
function CountDown(number)
{
console.log(number);
CountDown(number - 1);
}
*/
//this shit will never end
//
//the base case MUST ALWAYS be implemented for prevent things to call themselves indefinitely
function sum (low, high)
{
/*
if(high !== low)
return high + sum(low, high - 1);
else
return 1;
*/
if(high === low)
{
return low;
}
return high + sum(low, high - 1);
}
console.log(sum(1, 10));
console.log('----------------------------------');
let array =
[
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, 29]
], 30, 31
], 32
], 33
]
function RecursivePrint(array)
{
for(let item of array)
{
if(typeof(item) == "object")
{
RecursivePrint(item);
}
else
console.log(item);
}
}
RecursivePrint(array);
let arr = [2, 3, 4, 5];
function RecursiveDouble(array, index = array.length - 1)
{
if(index < 0)
{
return;
}
array[index] *= 2;
RecursiveDouble(array, index - 1);
}
RecursiveDouble(arr);
console.log(arr);
//What's great about top-down thinking: in a way, we can solve a problem without even knowing how to solve the problem