Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Maths/WhileLoopFactorial.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/*
author: Theepag
author: Theepag, optimised by merelymyself
*/
export const factorialize = (num) => {
// Step 1. variable result to store num
let result = num
// If num = 0 OR 1, the factorial will return 1
if (num === 0 || num === 1) { return 1 }
// Step 1. Handles cases where num is 0 or 1, by returning 1.
let result = 1
// Step 2. WHILE loop
while (num > 1) {
result *= num // or result = result * num;
num-- // decrement 1 at each iteration
result = result * num // or result = result * num;
}
// Step 3. Return the factorial
return result
Expand Down
12 changes: 12 additions & 0 deletions Maths/test/WhileLoopFactorial.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { factorialize } from '../WhileLoopFactorial'

function testFactorial (n, expected) {
test('Testing on ' + n + '!', () => {
expect(factorialize(n)).toBe(expected)
})
}

testFactorial(3, 6)
testFactorial(7, 5040)
testFactorial(0, 1)
testFactorial(12, 479001600)