0

Why output is 2 ?

Why output is 2 ? var arr = [1,3,4,2]; var count = 0; for(var x = 0;x <arr.length; x++) { x = arr[x]; count++; } document.write(count);

16th Sep 2018, 8:43 PM
Adams
4 Respuestas
+ 1
iteration | x | count ------------------------------------------------------------------------------ 1 | 0 | 0 2 | x = arr[0] = 1 | count = count + 1 = 1 | x = x + 1 = 2 | 3 | x = arr[2] = 4 | count = count + 1 = 2 | x = x + 1 = 5 |
16th Sep 2018, 9:10 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
Output is 5!
16th Sep 2018, 8:56 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
Thanks.
16th Sep 2018, 9:16 PM
Adams
- 1
Sorry my mistake,should be: document.write(count);
16th Sep 2018, 9:00 PM
Adams