Hello! First post here…
I seem to keep having a misunderstanding of whether to put things inside or outside of curly brackets. Could someone provide a good explanation of the difference in these two codes, particularly why “return result;” only works outside the brackets?
This works:
function factorialize(num) {
var result = num;
if(num===0){
return 1;
}
while(num>1){
num--;
result=result*num;
}
return result;
}
factorialize(5);
This does not work:
function factorialize(num) {
var result = num;
if(num===0){
return 1;
}
while(num>1){
num--;
result=result*num;
return result;
}
}
factorialize(5);