here is an error i <= inputNumber

should be i < inputNumber

var inputNumber = prompt('Please enter an integer');
var total = 1;

for (i = 0; i < inputNumber; i++){
    total = total * (inputNumber - i);
}

console.log(inputNumber + '! = ' + total);

Answer from Zhenya Telegin on Stack Overflow
🌐
OneCompiler
onecompiler.com › javascript › 3xdhgf2u8
Factorial of number using while loop - JavaScript - OneCompiler
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when ...
🌐
Studyfame
studyfame.com › javascript-program › find-factorial-number-program-in-javascript
Find factorial number program in JavaScript
<!DOCTYPE html> <html> <body> <script> function factorial() { var fact=1,i=1; // take input from user using prompt var a=prompt("enter a number:"); while(i<=a) { // calculate factorial of number fact=fact*i; i++; } // print the factorial value document.write("factorial of number "+a+" is:",fact); ...
🌐
Stack Abuse
stackabuse.com › calculate-factorial-with-javascript-iterative-and-recursive
Calculate Factorial With JavaScript - Iterative and Recursive
March 23, 2023 - Let's test our function in our browser's console and print out the result. Be sure to enter the factorial function in the browser's console first: var inp = window.prompt("Enter a number: "); inp = parseInt(inp); alert("The result is: " + getFactorialForLoop(inp));
Top answer
1 of 7
3

At first lets examine what went wrong:

var a = 1

What is a? Its definetly not a good name for a variable. Maybe name it to result ? The same applies to nth which should be named factorial and nth_fact which should rather be factorize or sth. You should also always use ; to end a statement.

while(nth_fact)

As your while loop contains multiple statements (the if and the two assignments) you need to open a block here by using { right after the condition. nth_fact refers to the function, you rather want to take factorial here.

 if (nth_fact == 0 || nth_fact == 1){
   break;

Now you open a block statement for the if, but you never close it. So you need another } after the break.

result => result * nth_fact
nth_fact => nth - 1
console.log() 

=> is the arrow function expression, but you want the assignment operator =. Also you need to pass something to console.log, e.g. console.log(result)

All together:

 function factorize(factorial){
   var result = 1;
  while(factorial){
     if (factorial == 0 || factorial == 1){
        break;
     }
     // ?
     factorial = factorial - 1;
     console.log(result);
  }
  return result;
}
2 of 7
1

That pseudocode is indeed confusing, because what it calls factorial is actually not the factorial -- it's the current value, which the result (which is actually the factorial we're looking for) is multiplied by. Also, if is superfluous, because while already checks for the same condition. So the correct pseudocode would be

currentValue = argument
factorial = 1

while (currentValue > 1)
    factorial = factorial * currentValue
    currentValue = currentValue - 1

// now, 'factorial' is the factorial of the 'argument'

Once you get this sorted out, here's a bonus assignment:

  • create a function range(a, b) that creates an array of numbers from a to b. For example, range(5, 8) => [5, 6, 7, 8]
  • create a function product(array) that multiples array elements by each other. For example, product([2, 3, 7]) => 42
  • write the factorial function using product and range
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › factorial-of-a-number-using-javascript
Factorial of a Number in JavaScript - GeeksforGeeks
This code defines a self-invoking function that returns a facto function, which calculates the factorial of a number while using caching for efficiency.
Published   July 31, 2025
🌐
freeCodeCamp
freecodecamp.org › news › how-to-factorialize-a-number-in-javascript-9263c89a4b38
Three Ways to Factorialize a Number in JavaScript
March 16, 2016 - function factorialize(num) { // Step 1. Create a variable result to store num var result = num; // If num = 0 OR num = 1, the factorial will return 1 if (num === 0 || num === 1) return 1; // Step 2. Create the WHILE loop while (num > 1) { num--; ...
Find elsewhere
🌐
Programiz
programiz.com › javascript › examples › factorial
JavaScript Program to Find the Factorial of a Number (with Examples)
Then if...else if...else statement ... shown. When the user enters 0, the factorial is 1. When the user enters a positive integer, a for loop is used to iterate over 1 to the number entered by the user to find the factorial....
🌐
GeeksforGeeks
geeksforgeeks.org › factorial-of-a-number-using-javascript
JavaScript – Factorial of a Number in JS | GeeksforGeeks
The while loop repeatedly multiplies result by n and decrements n until it reaches 1.
Published   January 20, 2025
🌐
Sololearn
sololearn.com › en › Discuss › 2734084 › factorial-fun-javascript
Factorial Fun - Javascript | Sololearn: Learn to code for FREE!
The variable "factorial" is assigned ... Without that, it would look something like this: number = prompt() for (; number > 0; number--) { number *= (number-1); } Imagine the user inputs 3 as the number....
🌐
T4Tutorials
t4tutorials.com › factorial-in-javascript-with-do-while-loop
Factorial In JavaScript with do while loop – T4Tutorials.com
Skip to content · Order PDF of any content from our website with a little minor Fee(starting from 3$) to donate for hard work. For details: contact whatsapp +923028700085 · Factorial In JavaScript with do while loop · By Prof. Dr. Fazal Rehman Shamil, Last Updated:March 3, 2022
🌐
Codingzap
codingzap.com › home › blog – programming & coding articles › javascript factorial calculator | finding factorial of a number
JavaScript Factorial Calculator | Finding Factorial Of A Number
April 1, 2025 - One is the Iterative Method & another is the Recursive Method. In the Iterative Method, you can use any Loop Concept of the JavaScript Programming Language. You can use the For Loop, While Loop, etc.
🌐
DEV Community
dev.to › cesareferrari › using-a-for-loop-to-output-a-factorial-4d6l
Using a for loop to output a factorial - DEV Community
December 11, 2019 - With all this in mind, let's write a loop that fulfills all these requirements: for(let i = 1; i <= integer; i++) { factorial *= i; } Well, our code is pretty short but as you can see it fulfills all our requirements.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › math › factorial of number
Calculate the factorial of a number using JavaScript - 30 seconds of code
August 18, 2024 - const factorial = n => { if (n < 0) throw new TypeError('Negative numbers are not allowed!'); let result = 1; for (let i = 2; i <= n; i++) result *= i; return result; }; factorial(6); // 720 ...
🌐
Reddit
reddit.com › r/learnprogramming › [javascript homework] creating a factorial program using a for loop
r/learnprogramming on Reddit: [JavaScript Homework] Creating a Factorial Program Using a For Loop
December 31, 2013 -

I have to write a factorial program. There is a hint that says: You solve factorial by multiplying the numbers from 1 all the way up to N together. You need a variable to store the result.

So I think I've done everything right except for storing the result as a variable. What do you guys think?

var N = 5;
function start(){
    for(var i = 1; i < N; i++){
        print(N *= i);    
    }
}

By the way, the problem was given with N = 5. Thanks for your help!

🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › factorial program in javascript
Factorial Program in JavaScript | How to find factorial program in javaScript
May 14, 2024 - The function calFact() will call itself until it reaches to value 1, but while passing the value it will pass it decreasing by 1 every time. It will go on multiplying with the original result till it reaches value 1. The final multiplying result ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Codecademy
codecademy.com › forum_questions › 54d28893d3292f20b7000ed8
Factorial Solution using While loop | Codecademy
def factorial(x): total = 1 while x > 0: total *= x x -= 1 return total ... In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.