/    Sign up×
Community /Pin to ProfileBookmark

Funcion/Array Question

Hello! I want to say thanks in advance for all the help you guys may provide. As you can definitely tell, I am new to JavaScript. I am using the JavaScript Bible, Six Edition. In chapter 7 (JavaScript Tutorial, Programming Fundamentals, Parallel Arrays), the book gives an example of the following parallel array:

var USStates = new Array(4);
USStates[0] = “Alabama”;
USStates[1] = “Alaska”;
USStates[2] = “Arizona”;

var stateEntered = new Array(4);
stateEntered[0] = 1819;
stateEntered[1] = 1959;
stateEntered[2] = 1912;

Later the book gives an example of a function which will give the user the ability to search within the array and find out when did the State entered the Union. So far, I think I have a good understanding of the array, the “onclick” element calling the function; however, I am stuck with the “if” portion of the function. I am trying to read and understand the explanation provided in the book, but I still don’t understand what the “[i]” within the “if” portion does or why it is there. The function is below:

function getStateDate() {
var selectedState = document.getElementById(“entry”).value;
for (var i = 0; i < USStates.length; i++) {
if (USStates[i] == selectedState) {
break;
}
}
alert(selectedState + ” entered the Union in ” + stateEntered[i] + “.”);
}

Again, thanks in advance for your help.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@sstalderMay 20.2008 — Here is a break down:
[code=php]
for (var i = 0; i < USStates.length; i++) {// Loop from 0 to max index of USStates array, in this case it would loop from 0 - 2
// So what really happens here:
//if (USStates[i] == selectedState) translates to...
//if ('Alabama' == selectedState)
//if ('Alaska' == selectedState)
//if ('Arizona' == selectedState)
// Since those are the only items in the array that is what it compares the value of 'selectedState' with
if (USStates[i] == selectedState) {// If a match is found then exit out of the loop
break;// Exit for loop
}
}
[/code]
Copy linkTweet thisAlerts:
@xtiano77authorMay 20.2008 — Thanks for your quick response. I am still not clear about what the "[i]" does? Could you break that down for me?
Copy linkTweet thisAlerts:
@xtiano77authorMay 20.2008 — Is the "[i]" function along with the "length" at the end searching the array by the length of each name? If so, what would happen if two states have the same ammount of letter and so their length is equal?
Copy linkTweet thisAlerts:
@baberliciousMay 20.2008 — the "[i]" isn't a function.. it is selecting the ith index of the USStates array.

For example,

USStates[0] is "Alabama"

USStates[2] is "Arizona"



the value of i comes from the FOR loop...

for (var i = 0; i < USStates.length; i++)

i = 0 and is i++ (increases by 1) until i is no longer < USStates.length..



basically the for loop will step through the USStates array untill it reaches the end, or until USStates[i] == selectedState as stated in the IF.. which causes the FOR to break..

then the... alert(selectedState + " entered the Union in " + stateEntered[i] + "."); will alert just what it says... where i will be = to the index of the USState where USState == selectedState... or in the event that the selectedState was not in the USState array, will be the last index of USState.



Hope this helps...
Copy linkTweet thisAlerts:
@sstalderMay 20.2008 — i is equal to the index of the array so if i type out USStates[0] that will return "Alabama", and so on.
Copy linkTweet thisAlerts:
@xtiano77authorMay 20.2008 — Steve,

Thank you so much. You have no idea how much your simple but effective instructions have helped me. It was getting pretty cloudy inside my head, but now the clouds have disipaged, the sun has come out and I am ready to continue with the book. Thanks again!
Copy linkTweet thisAlerts:
@xtiano77authorMay 20.2008 — Before I forget, I checked out some of your work and all I can say is that one day I would like to do that type of work. Nice work!
Copy linkTweet thisAlerts:
@sstalderMay 20.2008 — Thanks alot. Im sure one day it will all come together if you keep at it. Asking questions means your willing to learn so I can say that I think you are on the right track.
×

Success!

Help @xtiano77 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...