Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ![]() Interested in joining? Click here. If you are already a member of ZBCode, feel free to login right here: |
| Groups in ZB | |
|---|---|
| Tweet Topic Started: Jun 10 2009, 06:33 PM (597 Views) | |
| Reid | Jun 11 2009, 09:54 PM Post #21 |
![]()
What? The land of the free? Whoever told you that was your enemy.
![]()
|
Okay, so. You know what an array is right? If you don't... An array is a single variable that holds multiple pieces of data, almost like a filing cabinet and its many drawers. Its syntax is this: and to fill one in you simply use the same thing The number in the y[0] is called the index. The index always starts from 0. All arrays have a property called 'length', like y.length. In the above example, y.length is 5. This is because there are 5 pieces of data (cats, dogs, pigs, dolphins, and porpoises.) There are many other methods such as reverse, join, etc etc but we won't get into those. So, basically, when you use getElementsByTagName() it returns an array. Each piece of that array is an element. So if you had a few links on the page, we'll say 4, then this is what the array would look like: (pieces of data are separated by commas) Now all arrays have length, whether that length is 0 or not, they still have it. You'll notice that the last part of an array is always its length minus one. In the above example, the last part was at 4 (porpoises) and its length was 5. It always falls in this fashion. The -- is an operator, more specifically the decrement. Remember the +=, -=, *=, and /*? If you don't here is an example: The y++ is the same as y = y + 1, just like the y += 1 is the same as y = y + 1. -- is the same way. All three of these are the same thing. Two of them are the shortened form, and one of them is the long form. So. Let's loop through an array, shall we? Okay let's explain this. In that example, y.length is 4 because the array has 4 pieces of data in it (1, 2, 3, and 4, respectively.) So we assign that to variable z. Now, the syntax of a while loop is this: The wonderful thing about JS is that whatever you put in the condition, it will just do that as it checks the condition. So if we put z--, here's the process: 1. JS creates the array and gets it's length 2. It looks at the while loop 3. It looks at the condition. It then runs the condition once to check if it is true. 4. Since it is true, it does the code. 5. It repeats 3 through 4 again and again. So, before the while loop ever touches the code, it subtracts 1 from z. Remember back up there when I said the last part of an array is always 1 less than the length? Well, there you go. Now replace the z in the code with the actual number. That is what it will execute the first time. The second time? z will be 1 less than that. Then the next? and the next? And the next...? It won't run anything the next time. Why won't it? Because if you check 0 it returns false. That way it never runs the -1, because -1 can't exist in an array. (Actually, it can. But we're not going to go there right now.) That's why we use the [z] in there. Because that's the index of our array. Does that make sense? It's kind of a weird concept. |
| The Resource Board | |
![]() |
|
| Dorith | Jun 11 2009, 10:05 PM Post #22 |
![]()
Has just entered the Matrix
![]()
|
Ok I think I understand this. Tell me if I get this.
What that does is it says "while findname is checking all the way back to [0] (since the length is always the maximum right?), if you happen to find an "a" tag with Grey in it, switch it to sponsor, which is another variable." Is that what is happening? I realize that the length is always the last one. So you're here telling it to start at the end and go backwards until it find that. Then replace it. Is that what is happening in the code? Edit: Btw, that is an excellent explanation (if what I typed up there is right). For a newer person I'd suggest just explaining that .length always finds the last one. I think you should see if Dan would add what you wrote there in his JSchool thing on his website. It really is well explanation. Edited by Dorith, Jun 11 2009, 10:07 PM.
|
![]() (Made emoticon using Codes Rock's Smiley Generator) | |
![]() |
|
| Reid | Jun 11 2009, 10:29 PM Post #23 |
![]()
What? The land of the free? Whoever told you that was your enemy.
![]()
|
That is the gist of it. And no, the length is not always the maximum - the length-1 is the maximum. In the maximum is y[3], but y.length is 4. y[y.length] will return undefined or an error (one of the two) but y[y.length - 1] will give the last piece of data. Also, in this code: The code is basically doing this: 1. Define the array 2. Get the length --- start the while loop 3. Subtract 1 from the length 4. Check the new length to make sure it isn't 0 5. If isn't zero, see if y[_length] is cats 6. If it is cats, replace it with 'salami.' 7. Repeat 3 through 6 until we've gone through the entire array. So before we get to the while loop, _length is 4. But when it starts the while loop, it automatically subtracts one (thanks to the --) so it becomes 3, which is the last piece of data. Edited by Reid, Jun 11 2009, 10:30 PM.
|
| The Resource Board | |
![]() |
|
| Dorith | Jun 11 2009, 10:34 PM Post #24 |
![]()
Has just entered the Matrix
![]()
|
Oh alright I think I understand now! Thanks! Edit: Err... I just tried
And it didn't really change the members name to Grey Sponsor... Edited by Dorith, Jun 11 2009, 11:18 PM.
|
![]() (Made emoticon using Codes Rock's Smiley Generator) | |
![]() |
|
| Reid | Jun 12 2009, 01:40 PM Post #25 |
![]()
What? The land of the free? Whoever told you that was your enemy.
![]()
|
I apologize, it should have been this Since name[findname] is an element.
|
| The Resource Board | |
![]() |
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Code University · Next Topic » |


















7:47 PM Jul 10