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);
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 |
+ 2
Output is 5!
+ 2
Thanks.
- 1
Sorry my mistake,should be:
document.write(count);