I'm experimenting with recursive functions in javascript which takes a number and outputs the factorial of that number. I decided to go for simplicity and do something like this:
function myFunc(n)
{
if(n > 1){
return (n * myFunc(n - 1));
}
}
console.log(myFunc(4));
yet console.log() keeps outputing NaN. What am I doing wrong please? Everything looks right to me.
elsebranch of your code, wheren <= 1? tryelse return 1:Delsecase. That's why it is by default returningundefinedfor "else" andundefined+ a number =NaN.