Factorialize number a compressed solution

Just showing off this line :slight_smile: although it may have some drawbacks, and happy to hear any criticism.

const factorialize = (num) => num===0 ? 1: num*factorialize(num-1)
factorialize(5);

Everything is a tradeoff. Personally, I try to avoid recursive solutions if they aren’t needed - a simple loop works fine. Less chance of accidentally clogging up the call stack.

You could trim it down a little more as:

const factorialize = n => n ? n * factorialize(n - 1) : 1

Things like this can be good mental exercises, but I’d worry about decreasing readability. But maybe it doesn’t matter since it is just one line and the function name makes it clear what is happening.

2 Likes