0

why my output is 1

factorial using for loop https://code.sololearn.com/WbK6DIt8xvGO/?ref=app

17th Dec 2020, 6:35 AM
Aman Prasad
Aman Prasad - avatar
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
17th Dec 2020, 6:40 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 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);
17th Dec 2020, 8:38 AM
A͢J
A͢J - avatar
+ 1
thaks a lot
17th Dec 2020, 6:58 AM
Aman Prasad
Aman Prasad - avatar