Skip to content

Commit 7064149

Browse files
committed
feat: add first factorial solution
1 parent 8f9e9d4 commit 7064149

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

coderbyte/first-factorial/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# First Factorial
2+
3+
## Description
4+
5+
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 _ 3 _ 2 \* 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

coderbyte/first-factorial/solution.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function firstFactorial(num) {
2+
// code goes here
3+
let result = 1;
4+
while (num > 0) {
5+
result *= num;
6+
num--;
7+
}
8+
return result;
9+
}
10+
11+
module.exports = firstFactorial;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const firstFactorial = require('./solution');
2+
3+
describe('First Factorial', () => {
4+
const testCases = [
5+
{
6+
input: 4,
7+
output: 24,
8+
},
9+
{
10+
input: 8,
11+
output: 40320,
12+
},
13+
{
14+
input: 12,
15+
output: 479001600,
16+
},
17+
];
18+
19+
it('should return a number type', () => {
20+
expect(typeof firstFactorial(4)).toBe('number');
21+
});
22+
23+
it.each(testCases)('should return $output', (testCase) => {
24+
expect(firstFactorial(testCase.input)).toBe(testCase.output);
25+
});
26+
});

0 commit comments

Comments
 (0)