-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_recursion.js
More file actions
39 lines (33 loc) · 1.1 KB
/
5_recursion.js
File metadata and controls
39 lines (33 loc) · 1.1 KB
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
(
function(){
/*
Recursion is when a function calls itself, it's quite like a loop
*/
let countdown = (n) =>
{
console.log(n);
if (n<=0){
return 1;
}
n--;
return countdown(n);
}
countdown(10);
/*
To understand recursion you should understand what happens
when we call the upper-function `countdown`.
Let me tell you how that function gets executed:
First the function is called and the first line of code in the function
logs the argument into the console and returns 1 if the argument is less than or
equal to 0. then onto the next line the argument is decremented by 1.
And the function call is returned again, and again the same thing happens
until the n is less than or equal to 0.
Diagram:
call function(x):
log x
if x is less than or equal to 0 return 1
substract x by 1
if it isn't then decrement n and call function(x)
*/
}
)()