+ 1
Could someone explain me how this code works line to line? Thanks.
function f(n){ var res=1; for(var i=2;i<=n;++i){ res*=j; } return res; } document.write(f(4));
1 Resposta
+ 8
It should be res*=i.
f is called with 4, so n = 4.
Step 1:
res = 1 and i=2.
res *= i
=> res = res*i
=> res = 1*2 = 2
Step 2:
res = 2 and i = 3
res = 2*3 = 6
Step 3:
res = 6 and i = 4
res = 6*4 = 24
Now the loop condition becomes false since i is 5. So loop will stop here and the output will be 24