Skip to content

Commit a00ddd8

Browse files
committed
Done from section 8
1 parent eef9480 commit a00ddd8

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const factorial = (num) => {
2+
if (num < 0) {
3+
return 0;
4+
}
5+
6+
if (num === 1) return 1;
7+
8+
return num * factorial(num - 1);
9+
};
10+
11+
console.log(factorial(7));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Fibonacci sequence
2+
const fib = (num) => {
3+
if (num <= 2) {
4+
return 1;
5+
}
6+
7+
return fib(num - 1) + fib(num - 2);
8+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const power = (base, exponent) => {
2+
if (exponent === 0) {
3+
return 1;
4+
}
5+
6+
return base * power(base, --exponent);
7+
};
8+
9+
console.log(power(2, 4));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const productOfArray = (arr) => {
2+
if (arr.length === 0) {
3+
return 1;
4+
}
5+
6+
return arr[0] * productOfArray(arr.slice(1));
7+
};
8+
9+
console.log(productOfArray([1, 2, 3]));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const recursiveRange = (num) => {
2+
if (num === 0) {
3+
return 0;
4+
}
5+
6+
return num + recursiveRange(num - 1);
7+
};
8+
9+
console.log(recursiveRange(6));

0 commit comments

Comments
 (0)