Skip to content

Commit a60f486

Browse files
committed
Get factorial number
1 parent 14b96a8 commit a60f486

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/factorial/factorial.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Problem: return a factorial number of a given positive integer
2+
3+
// Loop approach:
4+
export const getFactorial = (num) => {
5+
let result = 1
6+
for (let i = 2; i <= num; i++) {
7+
result *= i
8+
}
9+
return result
10+
}
11+
12+
// Recursive approach:
13+
export const getFactorialRecursion = (num) => {
14+
if (num > 1) {
15+
return num * getFactorialRecursion(num - 1)
16+
} else {
17+
return 1
18+
}
19+
}

src/factorial/factorial.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getFactorial, getFactorialRecursion } from "./factorial";
2+
3+
describe("Factorial number", () => {
4+
test("Return factorial of 3", () => {
5+
expect(getFactorial(3)).toBe(6);
6+
});
7+
test("Return factorial of 6", () => {
8+
expect(getFactorial(6)).toBe(720);
9+
});
10+
});
11+
12+
describe("Factorial number | Recursive approach", () => {
13+
test("Return factorial of 3", () => {
14+
expect(getFactorialRecursion(3)).toBe(6);
15+
});
16+
test("Return factorial of 6", () => {
17+
expect(getFactorialRecursion(6)).toBe(720);
18+
});
19+
});

src/fibonacci-sequence/fibonacci-sequence.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Details: e.g. fibonacci sequence: 1,1,2,3,5,8,13,21...
33

44
// Time complexity: O(n)
5-
65
const getFibonacciEl = (i) => {
76
const fibonacciArr = [1, 1];
87
for (let x = 1; x < i; x++) {

0 commit comments

Comments
 (0)