Challenge Factorialize a Number has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
var total = 1;
function factorialize(num) {
for (var i=0; i<num; i++){
total = total * (num - i);
}
return total;
}
factorialize(5);
Duplicate of #9199
@LonePoop You just need to declare the 'total' variable inside the function scope.
function factorialize(num) {
var total = 1;
for (var i=0; i<num; i++){
total = total * (num - i);
}
return total;
}
factorialize(5);
Thanks guys!
Thanks all.
Closing in favor of the other thread.
RECURSION GUSY !!, https://msdn.microsoft.com/en-us/library/wwbyhkx4(v=vs.94).aspx
function factorialize(num) {
return (num !== 0) ? num * factorialize(num - 1) : 1;
}
factorialize(5);
Most helpful comment
@LonePoop You just need to declare the 'total' variable inside the function scope.
function factorialize(num) { var total = 1; for (var i=0; i<num; i++){ total = total * (num - i); } return total; } factorialize(5);