+ 1

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 My code: function main() { var number = parseInt(readLine(), 10) var factorial = 1; // controlle van sololearn for (; number >0; number--) { number*factorial } console.log (number); } The out put is 0 But why? What do I need to change to get the right number to show?

17th Jan 2021, 12:49 PM
Martijn Greven
13 Respostas
+ 13
U make some mistakes. U find the factorial but you didn't store in a variable. And u should print the factorial instead of number. Corrected code: var number = parseInt(prompt()); var factorial = 1; for (; number >0; number--) { factorial=number*factorial; } console.log (factorial);
17th Jan 2021, 1:04 PM
Vadivelan
+ 6
number*factorial does nothing because the result is not saved in a variable. In addition you want to change the variable number which is used in the loop. This make no sense here. The solution looks i.e. like as follows: for (; number>0; number--) { factorial *= number; } console.log (factorial);
17th Jan 2021, 1:16 PM
JaScript
JaScript - avatar
+ 6
function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for (i = 1; i <= number; number--) { factorial *= number; } console.log (factorial); }
4th Sep 2021, 1:53 PM
IbrahimCPS
IbrahimCPS - avatar
+ 3
function main() { var number = parseInt(readLine(), 10) var factorial = 1; for (; number >0; number--) { factorial=number*factorial; } console.log (factorial); }
26th Sep 2021, 7:08 AM
GUNASEKARA H.G.D.A.P. (CST18022)
GUNASEKARA H.G.D.A.P. (CST18022) - avatar
+ 2
Help me!!! Factorial The factorial of a number N is equal to 1 * 2 * 3 * ... * N For example, the factorial of 5 is 1* 2 * 3 * 4 * 5 = 120. Create a program that takes a number from input and output the factorial of that number. Use a for loop to make the calculation, and start the loop from the number 1. //IS JAVA
22nd Sep 2023, 7:33 PM
Juan Alburquerque
Juan Alburquerque - avatar
+ 1
using System; public class Program { static void Main(string[] args) { int num = Convert.ToInt32(Console.ReadLine()); int result =1; for (int i = 1; i <= num; i++) { result *= i; } Console.WriteLine( result); } }
23rd Feb 2024, 10:14 AM
Bhyravi Yogesh
Bhyravi Yogesh - avatar
0
I feel like I hacked CIA with this for loop setup: function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for (var keeper=number;--number>0;){ keeper*=number; } console.log(keeper) }
6th Aug 2021, 7:14 PM
Anastasiades Konstantinos
Anastasiades Konstantinos - avatar
0
I'd like this variant function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for ( let x = 1; x = number; number--) { factorial*=number; } console.log (factorial); }
30th Aug 2021, 8:39 AM
Maksym Tereshyn
Maksym Tereshyn - avatar
0
another possible solutions: function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for (var i=1;i<= number; i++) { factorial = factorial*i; } console.log (factorial) }
6th Sep 2021, 10:17 AM
Xavier
0
GUNASEKARA H.G.D.A.P. (CST18022) Hi Guna, would you please explain to me how you got this result? I lost it after « number —) ». The factorial part is messing with my brain. How factorial came from 1 to whatever it became after this ? Please help a lost soul lol
22nd Oct 2021, 11:14 AM
M M
M M - avatar
0
What da heel is Factoral Numbers????????????
16th Dec 2021, 12:26 PM
Milad Ahmad
Milad Ahmad - avatar
0
function main() { var number = parseInt(readLine(), 10) var factorial = 1; for (; number >0; number--) { factorial=number*factorial; } console.log (factorial); } Good Luck
25th Jan 2022, 8:08 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
Hi guys, I don't get why you are putting "number- -" on the code. I did the exercise with the following code and actually got the right answer. on the "number --" part I put "i ++". Can someone please help me with that. function main() { var number = parseInt(readLine(), 10) var factorial = 1; var i = 1; for(i ; i<= number; i++ ) { factorial*=i; } console.log(factorial) }
20th Mar 2022, 1:31 PM
Hugo Linhares