I have the following functions defined in JavaScript:
function rfactorial(n, f) {
if (n == 0) {
return f;
}
rfactorial(n-1, f*n);
}
function dofactorial(){
var n = prompt("Enter number");
var f = rfactorial(parseInt(n), 1);
alert("Factorial = " + f);
if (confirm("Do another one?")) {
dofactorial();
}
}
The problem is that in dofactorial(), f is undefined. This is strange since if I check the value of f in rfactorial() it is correctly calculated right before the return. What am I doing wrong?
returnat therfactorial(n-1, f*n);?