Skip to content

Commit 26c0a84

Browse files
feat: add recursion
1 parent 4a91251 commit 26c0a84

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
INFO: What is Recursion ?
3+
Recursion is when a function calls itself to solve a smaller version of a problem until it reaches a base case (a condition to stop calling itself)
4+
*/
5+
6+
function recursiveFunction() {
7+
if (baseCondition) {
8+
return result; // stop calling itself
9+
} else {
10+
return recursiveFunction(); // calls itself again
11+
}
12+
}
13+
14+
// Example
15+
function countdown(n) {
16+
if (n <= 0) {
17+
console.log("Done!");
18+
return;
19+
}
20+
21+
console.log(n);
22+
countdown(n - 1); // recursive call
23+
}
24+
countdown(3);

0 commit comments

Comments
 (0)