I dont Understand Loops and Arrays |
I dont Understand Loops and Arrays |
Corey Cody |
Apr 23 2012, 09:43 AM
Post
#1
|
Group: Members Posts: 9 Joined: 8-September 11 Member No.: 15,352 |
in my Advanced Web Design class in High School we are learning about Loops and Arrays. I have No idea what going on with Loops and arrays. my teacher gave a very complicated explanation of them and I was wondering if someone could Explain them Easier.
|
pandy |
Apr 23 2012, 10:50 AM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
I'm not the right person to explain, but I'll give it a try anyway. A loop is a peace of code that keeps repeating as long as certain conditions are met. Those conditions are stipulated when you set up the loop.
An array is similar to a variable, but it holds multiple items. Each item has its place in the array. You could define an array called 'fruit' and then stuff it with apples, pears, bananas and so on. You can then pick out each fruit by using its index number in the array. Apples will be fruit[0] (JavaScript counts from zero), pears will be fruit[1] and so on. |
Christian J |
Apr 23 2012, 03:36 PM
Post
#3
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
Can you give an example of what you didn't understand? Otherwise there are many general tutorials on the web, such as
http://www.javascriptkit.com/javatutors/loop2.shtml or http://www.w3schools.com/js/js_obj_array.asp http://www.w3schools.com/js/js_loop_for.asp |
Corey Cody |
Apr 24 2012, 10:03 AM
Post
#4
|
Group: Members Posts: 9 Joined: 8-September 11 Member No.: 15,352 |
I don't understand how to replace multiple variables like for instance:
Var names names[01] = Jack names[02] = Joe names[03] = sally with a for loop. |
Christian J |
Apr 24 2012, 12:26 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
Var names "var" must be in lower case. Also (at least with this kind of array syntax) you must declare it as an array, not just as a variable: CODE var names=new Array(); (BTW in many tutorials the length of the array is specified, like this: CODE var names=new Array(3); but that's optional, and frankly I don't see the point of it.) QUOTE names[01] = Jack String values (such as text) must be quoted: CODE names[01]='Jack'; Beginning all the index numbers with zeroes seems to work, but you might as well use just CODE names[1]='Jack'; Also keep in mind that array index numbering starts counting at zero, so currently the first item of the array above is undefined. QUOTE replace multiple variables [...] with a for loop. A loop doesn't replace an array, rather a loop can be used to walk through an array. Here's an example: CODE <script type="text/javascript"> var names=new Array(); names[01] = 'Jack'; names[02] = 'Joe'; names[03] = 'sally'; for(var i=0; i<names.length; i++) { document.write(names[i]+'<br>'); } </script> |
Lo-Fi Version | Time is now: 18th April 2024 - 10:43 PM |