0
How the Answer is 0?
var arr=[]; for(i=2;i>0;i--){ arr[i]=i;} document.write(arr.length* i); //Ans: 0
11 Answers
+ 7
Because i = 0, when you do arr.lenght*i you're multiplying by 0.
+ 3
If you create an array if length 3, it will have length 3 even if one or more elements are undefined.
The length of an array describes how many "slots" it has, independent of the data in it.
+ 2
Mohammad Tausiful Alam
Outside the loop i becomes 0 even though inside the loop it is reported as having a final value of 1. If you want the actual length of the array use:
document.write(arr.length);
not:
document.write(arr.length* i);
+ 1
On each iteration, print the array and i to see their values.
+ 1
Got it.I really appreciate all of u.
Thanks
+ 1
At the the of the for loop, the value of i becomes 0 (i--) , multiplying any number by 0 gives 0
+ 1
Good
0
Lisa, Edgar Sabido, Scott D
But console.log(arr[0]) is undefined.
Can undefined things be an array element?
0
Multiplying by 0 is always 0.
0
Get your out put inside the loop and try