I have a problem to understand why does RETURN in FOR loop returns me always initial fact value (let fact = 1):
const factorial = function(a) {
let fact=1;
if(a===0){
return 1;
}
else{
for(let i=1; i<=a; i++){
fact*=i;
return fact;** // This return
}
return fact
}
return fact;
};
//Result for all values e.g 1,5,10 is always 1, as in initial let fact =1
And when I remove it, my factorial function works totally fine. I have no idea why does it happen:
const factorial = function(a) {
let fact=1;
if(a===0){
return 1;
}
else{
for(let i=1; i<=a; i++){
fact*=i;
//Nothing
}
return fact
}
return fact;
};
// Code works fine, I receive factorial correctly
I was expecting to return factorial of my initial value. Instead I receive the initial variable value(1).