Why is my factorialization prompt not working please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Why is my factorialization prompt not working please?

https://code.sololearn.com/W2VV2NEzWsg4/?ref=app

25th Apr 2017, 12:49 PM
AceDev
4 Answers
+ 3
Why did you post many times the same question? ( I've already answered it in another more recent one ^^ )
25th Apr 2017, 4:27 PM
visph
visph - avatar
+ 1
I guess you are doing it wrong. You don't need a for-loop in factorial rather it should be recursive function: the function should be able call itself over and over until it hits the base condition: Check example below function factorial(x) { if (x === 1) { return 1; } return x * factorial(x - 1); } console.log(factorial(5)); // 5*4*3*2*1
25th Apr 2017, 1:22 PM
Benneth Yankey
Benneth Yankey - avatar
0
your loop is infinite
25th Apr 2017, 1:05 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
- 1
function fac(nu){ for (var i=nu-1;i>1;i--){ nu*=i; } return nu; } var nu= Number(prompt("Factorialize this number")); if (nu===0){ document.write(1); } document.write(fac(nu)); Just edit "if(nu===0);" to look like "if(nu==0);"
25th Apr 2017, 8:56 PM
listowel atiemo
listowel atiemo - avatar