- 1
Why is my factorialization prompt not working please?
4 Antworten
+ 3
Why did you post many times the same question? ( I've already answered it in another more recent one ^^ )
+ 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
0
your loop is infinite
- 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);"