Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fact = n => n < 1
- ? 1
- : Array(n)
- .fill(0)
- .map((_, i) => i + 1)
- .reduce((a, v) => a * v, 1);
- const countOf5 = n => Math.floor((n + 5) / 10);
- const countOf10 = n => Math.floor(n / 10);
- // step-by-step way
- const countOfZeros = n => countOf5(n) + countOf10(n);
- // the easiest way, count 5 and 10
- // const countOfZeros2 = n => Math.floor(n / 5);
- // prevent incorrect e-typed numbers
- for (let i = 0; i < 21; i++) {
- const zeros = countOfZeros(i);
- console.log(`factorial(${i}) = ${fact(i)} // ${zeros} zero${zeros % 10 === 1 ? '' : 's'}`);
- }
Add Comment
Please, Sign In to add comment