Javascript factorial The Coder on April 21, 2024 Hey everyone! I've stumbled upon this interesting JavaScript code that calculates the factorial of a number, and I'd love to hear your thoughts on... Read full post Submit Preview Dismiss Collapse Expand SaKKo SaKKo SaKKo Follow Geek is the new sexy. Location Thailand Education Mahidol University International College Work CEO at Geekstart Company Limited Joined Nov 4, 2018 • Apr 22 '24 Dropdown menu Copy link Hide const factorialMemo = {} function factorial(n) { if (n === 0 || n === 1) { return 1; } if (!factorialMemo[n]) { factorialMemo[n] = n * factorial(n - 1) } return factorialMemo[n]; } Enter fullscreen mode Exit fullscreen mode For me, I would prefer to memoize the result. I'm not too worried about coding styles. As long as it is easy to read and it's correctly computed. Collapse Expand The Coder The Coder The Coder Follow Joined Feb 28, 2024 • Apr 22 '24 Dropdown menu Copy link Hide There are many coding styles and it is worth noting that one size doesn't fit all. So, it is totally ok to have your own coding style as long as the code follows the code practices and industry standards. Collapse Expand SaKKo SaKKo SaKKo Follow Geek is the new sexy. Location Thailand Education Mahidol University International College Work CEO at Geekstart Company Limited Joined Nov 4, 2018 • Apr 22 '24 Dropdown menu Copy link Hide My point is just to improve performance. The Coder The Coder The Coder Follow Joined Feb 28, 2024 • Apr 22 '24 Dropdown menu Copy link Hide I get it Collapse Expand Eckehard Eckehard Eckehard Follow Always curious... Location Germany Work Numerical simulation specialist, IoT developer Joined May 24, 2021 • Apr 21 '24 • Edited on Apr 21 • Edited Dropdown menu Copy link Hide You can use a simple loop, but recursion can be shorter: // Loop function fac1(n) { let k = 1 for (let i = 1; i <= n; i++) k = k * i return k } Enter fullscreen mode Exit fullscreen mode With respect to your code: Why not check for (n <= 1) ? And this is a perfect case for the ternary operator, so your code could look like this: function factorial(n) { return (n <= 1) ? 1 : n * factorial(n - 1); } Enter fullscreen mode Exit fullscreen mode Collapse Expand The Coder The Coder The Coder Follow Joined Feb 28, 2024 • Apr 22 '24 Dropdown menu Copy link Hide Smart thinking. Collapse Expand Eckehard Eckehard Eckehard Follow Always curious... Location Germany Work Numerical simulation specialist, IoT developer Joined May 24, 2021 • Apr 22 '24 Dropdown menu Copy link Hide Thank you much! Talking about improvements: else after return is superfluous. So, your unchanged code should look like this: function factorial(n) { if (n === 0 || n === 1) return 1; return n * factorial(n - 1); } Enter fullscreen mode Exit fullscreen mode And your code is a bit dangerouse: see what happens, if you try factorial(-1). Checking for <=1 covers this too.... The Coder The Coder The Coder Follow Joined Feb 28, 2024 • Apr 22 '24 Dropdown menu Copy link Hide Thank you for pointing this out to me. It should cover negative numbers Collapse Expand Jon Randy ποΈ Jon Randy ποΈ Jon Randy ποΈ Follow π€ Artisanal developer - coding with varying degrees of success since 1983 Location Bangkok πΉπ Joined Jun 1, 2018 • Apr 22 '24 • Edited on Apr 22 • Edited Dropdown menu Copy link Hide π // Loop const fac = (n, k=1) => {for (;n--;k*=n+1){} return k} Enter fullscreen mode Exit fullscreen mode Collapse Expand The Coder The Coder The Coder Follow Joined Feb 28, 2024 • Apr 22 '24 Dropdown menu Copy link Hide Smart thinking @jonrandy Collapse Expand Amin Amin Amin Follow Education Γcole SupΓ©rieure de GΓ©nie Informatique Work Teacher, Freelance Developer & Chief Technical Officer @ Doc2wheels Joined Aug 17, 2018 • Apr 22 '24 Dropdown menu Copy link Hide Code of Conduct • Report abuse
For me, I would prefer to memoize the result. I'm not too worried about coding styles. As long as it is easy to read and it's correctly computed.
There are many coding styles and it is worth noting that one size doesn't fit all. So, it is totally ok to have your own coding style as long as the code follows the code practices and industry standards.
My point is just to improve performance.
I get it
You can use a simple loop, but recursion can be shorter:
With respect to your code: Why not check for (n <= 1) ? And this is a perfect case for the ternary operator, so your code could look like this:
Smart thinking.
Thank you much!
Talking about improvements: else after return is superfluous. So, your unchanged code should look like this:
And your code is a bit dangerouse: see what happens, if you try
factorial(-1). Checking for <=1 covers this too....Thank you for pointing this out to me. It should cover negative numbers
π
Smart thinking @jonrandy