Created
July 1, 2020 13:21
-
-
Save joefearnley/d0a5ef02cb545b5afd513031ac6c2ae7 to your computer and use it in GitHub Desktop.
The 10-Day JavaScript Challenge - Day 7: Factorial Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// basic with for loop | |
const factorialNumber = num => { | |
let result = 1; | |
for(let i = result + 1; i <= num; i++) { | |
result *= i; | |
} | |
return result; | |
}; | |
// refactored w/ higher order functions | |
const factorialNumber = num => [...Array(num).keys()] | |
.map(n => n + 1) | |
.reduce((a, c) => a * c, 1); | |
describe('factorialNumber()', () => { | |
it('returns factorial of number param', () => { | |
// arrange | |
const num = 5; | |
// act | |
const result = factorialNumber(num); | |
// log | |
console.log("result: ", result); | |
// assert | |
expect(result).toBe(120); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment