+ 3
why the output =0 var arr=[1,2,3,4]; var x=1; for(var i=0;i<arr.length ;i++){ x*=i; } alert (x);
6 Antworten
+ 5
In first iteration : x=x*0 that is x=0
And then in every iteration X will be 0.
For more information do debugging in angularjs using any IDE.
+ 13
i =0 -->0*x = 0 :)
+ 3
thanks
+ 3
The reason is because each time the loop iterates through the that array, it multiplies a number by zero and saves that result. No matter how many numbers you multiply by zero, it will always be zero.
+ 3
Your loop starts with I equal zero. zero times anything is zero so the first thing your program does is set X equal to zero, from then on it's always zero times a number which comes out to be zero again.
+ 1
Because any numbers multiplied with zero is always zero. you better to initialise i=1 inside the for loop to get a different output