Jump to content

JavaScript help


houssam_ballout

Recommended Posts

Hello all, can some body explain, how this code works/ (more on var x)thanks<script type="text/javascript">var xvar mycars = new Array()mycars[0] = "Saab"mycars[1] = "Volvo"mycars[2] = "BMW"for (x in mycars){document.write(mycars[x] + "<br />")}</script>
Do you know how for loops work? The equivilant of that is this:<script type="text/javascript">mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]="BMW"for(x=0;x<mycars.length;x++) { document.write(mycars[x] + "<br />")}</script>The first loop (not mine, the one part of the W3S tutorial) is a simplified way to loop through the contents of an array and access them through array[var], where in this case, var is x. :)
Link to comment
Share on other sites

Do you know how for loops work? The equivilant of that is this:<script type="text/javascript">mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]="BMW"for(x=0;x<mycars.length;x++) { document.write(mycars[x] + "<br />")}</script>The first loop (not mine, the one part of the W3S tutorial) is a simplified way to loop through the contents of an array and access them through array[var], where in this case, var is x. :)
The for..in version also seems to be a lot less strict. I typed in
<html><body><script type="text/javascript">var xvar mycars = new Array()mycars[2] = "BMW"mycars[0] = "Saab"for (x in mycars){document.write(mycars[x] + "<br />")}</script></body></html>

and it spit outBMWSaabjust in the order I typed them above, while the for loop version would not tolerate this and would spit outSaabundefinedBMWlining them up numerically and taking note of the fact that 1 is missing from the array. So the for..in version seems a lot more lienient. Are there any other differences?PS: Um... why aren't the BB tags for posting code snippets working?

Link to comment
Share on other sites

PS: Um... why aren't the BB tags for posting code snippets working?
I think you put [\code] rather than [/code].The for-in loops appear to iterate through all the elements of an object whereas the typical for loops iterate using in indexer (typically it's called "i") like in Chocolate570's example.In the try-it example (http://www.w3schools.com/js/tryit.asp?filename=tryjs_array_for_in), the x takes on the value of 0 in the first loop, 1 in the second, and 2 in the third as it iterates through the array.However, like ViolaGirl mentioned, if you had this:
var xvar mycars = new Array()mycars[0] = "Saab"mycars[2] = "BMW"mycars[1] = "Volvo"for (x in mycars){document.write(mycars[x] + "<br />")}

The first time through the loop, x would be 0, the second time it would be 2, and the third time it would be 1.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...