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));
๐ŸŒ
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--; ...
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
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 - We are going to first discuss the Factorial Calculation Method in Iterative Approach. Apart from JavaScript, you must be learning HTML and CSS courses. But solving HTML & CSS assignments could be challenging and in this case, you can ask for HTML assignment help. Now, in this Iterative Approach, the For Loop will be used to get the required result. Instead of the For Loop, you can use the While Loop & Do-While Loop as well.
๐ŸŒ
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
January 1, 2014 -

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!

๐ŸŒ
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.