+ 1

Help expantiate please! What's the output and why?

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

18th Feb 2018, 9:46 PM
BriaŚ”‎
6 Answers
+ 6
the result is 2. The explication to will be given with simple debug. Debug: 1 | <Declaration of the array "arr"> 2 | <Declaration of the variable "count"> 3 | 4 | <for loop ,condition: x < arr.length ,enter loop> 5 | <x receive arr[x]><x = 1> 6 | <count receive count + 1><count = 1> <end loop x receive x + 1><x = 2> 4 | <for loop ,test: x < arr.length ,true then enter loop> 5 | <x receive arr[x]><x = 3> 6 | <count receive count + 1><count = 2> <end loop x receive x + 1><x = 4> 4 | <for loop ,test: x < arr.length ,false break loop> 7 | <write 2> (Sorry my bad English)
19th Feb 2018, 12:59 AM
J G
J G - avatar
+ 8
There is so much wrong with that code, you get an error. This I think is what you may want. Notice the differences: var x = [1, 3, 4, 2]; var count = 0; for ( i = 0; i < x.length; i++ ) { x[i] = i; count++; } document.write(count);
18th Feb 2018, 10:02 PM
J.G.
J.G. - avatar
+ 5
It will throw and error because "arr" is not defined.
18th Feb 2018, 10:02 PM
Toni Isotalo
Toni Isotalo - avatar
+ 4
Sorry guys, I've modified the question. your responses are highly valued!
18th Feb 2018, 10:11 PM
BriaŚ”‎
+ 2
Nothing would really be the output of this code besides just an error, for a few reasons. The first is that in the last line, there is no semicolon and instead a period which just results in an error as the program doesn't really know what to do with that. The second one is that the arr variable you're trying to reference to in the for loop doesn't actually exist. Because of this, the for loop can't run as the second condition, x being less than the length of arr, cannot be run as it doesn't exist. The last reason is that you're trying to use x for way too many things throughout your code, and in places where it cannot be used. When x is first declared, it is created as an array, meaning it becomes a container of many different values all in one variable. In the for loop, however, you try to use x as an int, testing its value to see if it's less than the length of the arr variable (that doesn't exist), not allowing the for loop to run. Then, if the for loop had by some chance run, the value of x would then be set to a corresponding value in the arr variable, and had it run, would have messed up the entire for loop as it basically relies on x being a specific data type.
18th Feb 2018, 10:02 PM
Faisal
Faisal - avatar
+ 2
Yep, now it's better. First, you once again forgot to add the semicolon at the end, which results in an error. Second, with that being fixed, the output will then become 2. When the for loop is running, everytime the condition in the brackets evaluates to true, the value of x then becomes the value of whatever number is at the index of the current value of x, with the first iteration resulting in x being equal to 1 as the number at the index 0 (which is the value of x) is 1. Then, after the value of x is reset, the value of the variable count gets incremented by one. Going through the loop, in the second iteration, the value of x becomes the value of the index value of 1 in the array, which results in x being equal to 3, and count going up once more, now being equal to 1. For the third time, x becomes equal to the value of the variable at the third index, resulting in 2, and count once again being incremented, now being equal to 2. Lastly, the value of x is reassigned to the second index value in the array, now being equal to 4, which results in the for loop being terminated as x is now greater than the length of the array. Because of this, it runs the last line (if the semicolon were there), which prints out the value of count, printing out 2. Hope this helped!
18th Feb 2018, 10:21 PM
Faisal
Faisal - avatar