Fun Stuff > CLIKC
Need help (lots) with Javascript
Sorflakne:
Missed the above post because I decided to reformat my computer (and ran into...issues, but they're all better now!), however, I do have a question on arrays...
So here's a basic array:
<script>
var i;
var cars = new Array();
cars[0] = "Saab";
cars[1] = "Volvo";
cars[2] = "BMW";
for (i=0;i<cars.length;i++)
{
document.write(cars[i*] + "<*br>");
}
</script>
(*s are in the block to prevent the line from messing up, disregard them)
What does the line "for (i=0;i<cars.length;i++)" mean? I know why it's there, but I don't quite know how to interpret it. Is this format universal for arrays, or does it vary depending on whether the array is 1D or 2D? And why is i less than cars.length?
Funny enough, the above paragraph was a lot longer, but I answered a lot of it myself as I was writing my questions out, lol.
cesium133:
That's the notation for a loop. It starts the variable i equal to zero, then runs the command inside the loop, adds 1 to i, then checks whether i is less than the length of the cars array. It repeats the command until i is equal to or greater than the length of the cars array.
The basic syntax is
for(initial condition; test; increment command)
{
command(s) to be repeated
}
pwhodges:
And the reason for using < "less than" is that for three elements we are using the indices 0, 1 and 2 - but not 3, so when i=3 (i<3 is no longer true) we need to end the loop. cars.length is the number of elements in cars, unsurprisingly.
This format of loop was introduced in C in 1972.
snalin:
So, what happens is:
You make a variable named "i", and assign 0 to it. i is now 0.
Every time, before you do what is in the loop*, the conditional (i < cars.length) is tested. If it evaluates to false (i is larger or equal to the length of cars), the loop is not run, and the code passes on. This means that if you hadn't added anything to cars, so cars is 0, the stuff in the loop never happens. Note that you don't have to compare to the length of cars, you could put 3 there, and it would do the same thing. If you wrote i < 2, it would only print out "Saab" and "Volvo".
Every time, after you do what is in the loop, the value of i is incremented by one**.
Something which will do the exact same as what you did above, but is more easily readable, is:
--- Code: ---var i = 0;
var cars = new Array();
cars[0] = "Saab";
cars[1] = "Volvo";
cars[2] = "BMW";
while(i < cars.length) {
document.write(cars[i] + "<*br>");
i++;
}
--- End code ---
With regards to your question about whether it is universal for arrays, yes. If you have a 2d array, that is simply an array that contains arrays in every cell; an array of arrays. You can go down that rabbit hole as far as you want; 3 dimensional or 4 dimensional arrays are totally possible. You can also (in JS) have arrays that contains arrays in some cells, numbers in others and strings in yet others, this is a horrible idea.
I cooked up an example. The only thing of note that might not be straightforward is the use of console.log instead of document.write. It logs to your console, which can be found in chrome with ctrl+shift+j, or in firefox with ctrl+shift+k. It allows you to check what's happening in your code without editing how your web page looks, so you'll need to do that later on, so it's smart to start out doing it now.
So, example of use of 2d array, with 2d lookup. Copy it to a text document, put <html> and <script> tags around it, and open it in your browser of choice. Then open the console and see what a horrible person Tina is. Play around with it!
--- Code: ---var persons = new Array();
var tom = new Array();
tom[0] = "Tom";
tom[1] = "www.Tom.com";
tom[2] = "Lasanga";
var carl = new Array();
carl[0] = "Carl";
carl[1] = "www.Carl.com";
carl[2] = "Pizza";
var tina = new Array();
tina[0] = "Tina";
tina[1] = "www.Tina.com";
tina[2] = "The Blood of the innocent";
persons[0] = tom;
persons[1] = carl;
persons[2] = tina;
for(var i = 0; i < persons.length; i++) {
console.log("Person number " + (i+1) + ":");
console.log("Name: " + persons[i][0]);
console.log("Web Page: " + persons[i][1]);
console.log("Favourite Dish: " + persons[i][2]);
console.log("\n"); //New line
}
--- End code ---
*document.write(cars) etc. You can put as many lines as you want in there. You can also assign new values to i, which will do funny things to your code.
** These are equivalent:
--- Code: ---i++;
i += 1;
i = i+1;
--- End code ---
EDIT: offer to guide you though some examples over voice chat still stands! Or text chat if you are shy. Or just post more questions here. But I'd love to talk you through some stuff! I'm going to be a teachers assistant at basic level Java this semester, and I've only done higher levels before, so some experience with someone who's entirely fresh would be cool.
Sorflakne:
Ok, so it's safe to assume that i=0 by default all the time then?
@snalin: Eh, probably stick to posts for now. Your example array is interesting, I'll have to mess around with it more before I feel confident on it, but it definitely made array structure clearer for me.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version