Skip to content

Commit 8e46e41

Browse files
committed
recursive function practice 🔃
1 parent d71a276 commit 8e46e41

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 1
2+
function countFuction(num) {
3+
console.log(num);
4+
if (num <= 0) {
5+
return;
6+
}
7+
8+
countFuction(num - 1);
9+
}
10+
11+
countFuction(8);
12+
13+
// 2
14+
function anotherAountFuction(num) {
15+
console.log(num);
16+
if (num > 0) {
17+
anotherAountFuction(num - 1);
18+
}
19+
}
20+
21+
anotherAountFuction(8);
22+
23+
// sum of digits
24+
function sumOfDigits(num) {
25+
if (num == 0) {
26+
return 0;
27+
}
28+
return (num % 10) + sumOfDigits(Math.floor(num / 10));
29+
}
30+
31+
let sum = sumOfDigits(324);
32+
console.log(`res = ${sum}`);
33+
34+
// factorial
35+
var factorial = function (number) {
36+
// break condition
37+
if (number <= 0) {
38+
return 1;
39+
}
40+
41+
// block to execute
42+
return number * factorial(number - 1);
43+
};
44+
45+
console.log(`Factorial = ${factorial(9)}`);

0 commit comments

Comments
 (0)