+ 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);

22nd Sep 2018, 7:49 PM
olbab omer
olbab omer - avatar
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.
23rd Sep 2018, 6:40 AM
Jai Verma
Jai Verma - avatar
+ 13
i =0 -->0*x = 0 :)
23rd Sep 2018, 9:18 AM
Lương Văn Tuấn
Lương Văn Tuấn - avatar
+ 3
thanks
22nd Sep 2018, 7:57 PM
olbab omer
olbab omer - avatar
+ 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.
23rd Sep 2018, 7:14 AM
Adam Schork
Adam Schork - avatar
+ 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.
24th Sep 2018, 6:34 AM
Rick Shiffman
Rick Shiffman - avatar
+ 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
22nd Sep 2018, 7:53 PM
Arjun gowda
Arjun gowda - avatar