Skip to content

Instantly share code, notes, and snippets.

@joefearnley
Created July 1, 2020 13:21
Show Gist options
  • Save joefearnley/d0a5ef02cb545b5afd513031ac6c2ae7 to your computer and use it in GitHub Desktop.
Save joefearnley/d0a5ef02cb545b5afd513031ac6c2ae7 to your computer and use it in GitHub Desktop.
The 10-Day JavaScript Challenge - Day 7: Factorial Number
// 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