0

How the Answer is 0?

var arr=[]; for(i=2;i>0;i--){ arr[i]=i;} document.write(arr.length* i); //Ans: 0

11th Jan 2023, 12:50 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
10 Answers
+ 7
Because i = 0, when you do arr.lenght*i you're multiplying by 0.
11th Jan 2023, 1:55 PM
Edgar Sabido
Edgar Sabido - avatar
+ 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.
11th Jan 2023, 4:07 PM
Lisa
Lisa - avatar
+ 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);
11th Jan 2023, 1:54 PM
Scott D
Scott D - avatar
+ 1
On each iteration, print the array and i to see their values.
11th Jan 2023, 1:08 PM
Lisa
Lisa - avatar
+ 1
Got it.I really appreciate all of u. Thanks
11th Jan 2023, 4:10 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
+ 1
At the the of the for loop, the value of i becomes 0 (i--) , multiplying any number by 0 gives 0
12th Jan 2023, 9:23 AM
Omanshu
Omanshu - avatar
+ 1
Good
12th Jan 2023, 6:30 PM
Muhammadali Ergashaliyev
Muhammadali Ergashaliyev - avatar
0
Lisa, Edgar Sabido, Scott D But console.log(arr[0]) is undefined. Can undefined things be an array element?
11th Jan 2023, 3:52 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
0
Multiplying by 0 is always 0.
13th Jan 2023, 4:46 AM
Arafat Hossain Ar
Arafat Hossain Ar - avatar
0
Get your out put inside the loop and try
13th Jan 2023, 6:10 AM
Ayesh Chamodye
Ayesh Chamodye - avatar