← Back to Community
##### How would I make a factorial calculator function?
Profile icon
RohilPatel

How would I make a factorial calculator function? I need that to make a permutations and combinations calculator. I am pretty sure you would need a loop of some sort, but I'm not sure how to do it. Thanks for the help!

Voters
Profile icon
Profile icon
CodyChoo
Profile icon
RohilPatel
hotnewtop
Profile icon
[deleted]

have you tried googling? I found this quite easily: https://www.sitesbay.com/javascript/javascript-factorial-of-number
Do you know about recursion? It can be used instead of a loop: https://www.w3resource.com/javascript-exercises/javascript-recursion-function-exercise-1.php

Profile icon
RohilPatel

Sort of but not much luck , @malvoliothegood

Profile icon
mwilki7

factorials: n × (n - 1) × (n - 2) ... 2 × 1

you can try using for loop variable to start at `n` and go to `1`
and get something to hold the product of the multiplication inside the loop

Profile icon
RohilPatel
Profile icon
mwilki7

@RohilPatel
this is how you can implement the factorial function on your calculator

Profile icon
RohilPatel

How do get the number of numbers before hitting 1? @mwilki7

Profile icon
SixBeeps
Profile icon
mwilki7

@RohilPatel

```.css-19sk4h4{position:relative;}.css-1bu6gr6{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:relative;}.css-1n2m10r{padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1hwur6u{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1ubbl1f{padding:var(--space-4);padding-left:var(--space-4);padding-right:var(--space-2);font-family:var(--font-family-code);font-size:14px;line-height:var(--line-height-small);overflow-x:auto;word-break:break-word;white-space:break-spaces;overflow-wrap:anywhere;}```let product = 1;
for (let i = 0; i < n; i++)
{
product *= i;
}``````
Profile icon
RohilPatel

Is n the number ur "factorialing" by? @mwilki7

Profile icon
mwilki7

@RohilPatel
yes, if i want to find the factorial of 5, n would be 5

Profile icon
RohilPatel

Hmm, it's not working. @mwilki7

Profile icon
mwilki7

@RohilPatel
what are the results you are getting

Profile icon
RohilPatel

Try it ur self. @mwilki7

Profile icon
SixBeeps

@RohilPatel Make the iterator start from 1 and not 0. Multiplication by 0 just makes the product, well, 0.

Profile icon
mwilki7

@SixBeeps
shoot how did i miss that

Profile icon
SixBeeps

@mwilki7 Don't worry, I spent all morning today wondering why my webpage wasn't running. Turns out I spelled "replace" as "replase" :/