0
why my output is 1
factorial using for loop https://code.sololearn.com/WbK6DIt8xvGO/?ref=app
3 Antworten
+ 5
In this line,
for( i=1;i<=n;i++){
k=k*i;
return(k);
}
here first it will go to the for loop here k=k*i=(1*1)=1
then it will return 1 and the function will not work anymore.
If you want to get expected result, you should return it after the loop.
Like this:
https://code.sololearn.com/Wa24a3a11A6A
+ 3
Aman Prasad
Using recursion:
function fact(n){
if (n == 1)
return 1;
else
return n * fact(n - 1);
}
var f = fact(6);
document.write(f);
+ 1
thaks a lot