Factorial Fun - Javascript
The case: A number's factorial is the product of all positive integers less than or equal to the number. Write a program that takes a number as input and outputs its factorial to the console. Sample Input 5 Sample Output 120 Explanation 5*4*3*2*1 = 120 The answer: var number = parseInt(prompt()); var factorial = 1; for (; number >0; number--) { factorial=number*factorial; } console.log (factorial); I just cant understand the reason why the answer is what it is. I copied it from another post. I don't understand why we need the variable factorial So number is the input to find the factorial of the number So why do we start with a a variable called factorial. Because we always need a starting point which we can identify as 1? Why isnt the starting point actually number? as this is actually what we are stating with and using in the loop. I'm not sure if you understand what I'm trying to ask but if anyone can give more of an explanation on how the variables are decided that would be really helpful.