/    Sign up×
Community /Pin to ProfileBookmark

function to look out for arrays

hi, im new to javascript.
i cant get my script to work. I would like that if i type a number in the text box and click the button, it searches in the 2 arrays for the corresponding number and displays in an alert message. for example if I type 2 it should say “Arizona entered the union in 1902”. thanks in advance.

—–Code—-

<html>
<head>
<title>Untitled Document</title>
<script type=”text/javascript”>
<!–
var USStates = new Array(51);
USStates [0] = “Alabama”;
USStates [1] = “Alaska”;
USStates [2] = “Arizona”;
USStates [3] = “Arkansas”;
USStates [4] = “California”;
USStates [5] = “Colorado”;
USStates [6] = “Connecticut”;
USStates [7] = “Delaware”;
USStates [8] = “Florida”;
USStates [9] = “Georgia”;
USStates [10] = “Hawaii”;
USStates [11] = “Idaho”;
USStates [12] = “Illinois”;
USStates [13] = “Indiana”;
USStates [14] = “Iowa”;
USStates [15] = “Kansas”;
USStates [16] = “Kentucky”;
USStates [17] = “Louisiana”;
USStates [18] = “Maine”;
USStates [19] = “Maryland”;
USStates [20] = “Massachusetts”;
USStates [21] = “Michigan”;
USStates [22] = “Minnesota”;
USStates [23] = “Mississippi”;
USStates [24] = “Missouri”;
USStates [25] = “Montana”;
USStates [26] = “Nebraska”;
USStates [27] = “Nevada”;
USStates [28] = “New Hampshire”;
USStates [29] = “New Jersey”;
USStates [30] = “New Mexico”;
USStates [31] = “New York”;
USStates [32] = “North Carolina”;
USStates [33] = “North Dakota”;
USStates [34] = “Ohio”;
USStates [35] = “Oklahoma”;
USStates [36] = “Oregon”;
USStates [37] = “Pennsylvania”;
USStates [38] = “Rhode Island”;
USStates [39] = “South Carolina”;
USStates [40] = “South Dakota”;
USStates [41] = “Tennessee”;
USStates [42] = “Texas”;
USStates [43] = “Utah”;
USStates [44] = “Vermont”;
USStates [45] = “Virginia”;
USStates [46] = “Washington”;
USStates [47] = “West Virginia”;
USStates [48] = “Wisconsin”;
USStates [49] = “Wyoming”;
USStates [50] = “Virgin Islands”;

var stateEntered = new Array(51);
stateEntered [0] = “1900”;
stateEntered [1] = “1901”;
stateEntered [2] = “1902”;
stateEntered [3] = “1903”;
stateEntered [4] = “1904”;
stateEntered [5] = “1905”;
stateEntered [6] = “1906”;
stateEntered [7] = “1907”;
stateEntered [8] = “1908”;
stateEntered [9] = “1909”;
stateEntered [10] = “1910”;
stateEntered [11] = “1911”;
stateEntered [12] = “1912”;
stateEntered [13] = “1913”;
stateEntered [14] = “1914”;
stateEntered [15] = “1915”;
stateEntered [16] = “1916”;
stateEntered [17] = “1917”;
stateEntered [18] = “1918”;
stateEntered [19] = “1919”;
stateEntered [20] = “1920”;
stateEntered [21] = “1921”;
stateEntered [22] = “1922”;
stateEntered [23] = “1923”;
stateEntered [24] = “1924”;
stateEntered [25] = “1925”;
stateEntered [26] = “1926”;
stateEntered [27] = “1927”;
stateEntered [28] = “1928”;
stateEntered [29] = “1929”;
stateEntered [30] = “1930”;
stateEntered [31] = “1931”;
stateEntered [32] = “1932”;
stateEntered [33] = “1933”;
stateEntered [34] = “1934”;
stateEntered [35] = “1935”;
stateEntered [36] = “1936”;
stateEntered [37] = “1937”;
stateEntered [38] = “1938”;
stateEntered [39] = “1939”;
stateEntered [40] = “1940”;
stateEntered [41] = “1950”;
stateEntered [42] = “1951”;
stateEntered [43] = “1952”;
stateEntered [44] = “1953”;
stateEntered [45] = “1954”;
stateEntered [46] = “1955”;
stateEntered [47] = “1956”;
stateEntered [48] = “1957”;
stateEntered [49] = “1958”;
stateEntered [50] = “1959”;

function getStateDate(entry) {
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] + “.”);
}
// –>
</script>
</head>

<body>
<form>
<input type=”text” name=”entry” id=”entry” />
<input type=”button” value=”click” onchange=”getStateDate()” />
</form>
<script type=”text/javascript”>
<!–
// –>
</script>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJan 17.2009 — Don't really need to use 2 different arrays if you approach the problem like this:

I don't think the dates the states entered the union is correct, so I made up my own message. ?
[code=php]
<html>
<head>
<title>States / Year</title>
<script type="text/javascript">
var USStates = [
["Alabama","1900"],

["Alaska","1901"],

["Arizona","1902"],

["Arkansas","1903"],

["California","1904"],

["Colorado","1905"],

["Connecticut","1906"],

["Delaware","1907"],

["Florida","1908"],

["Georgia","1909"],

["Hawaii","1910"],

["Idaho","1911"],

["Illinois","1912"],

["Indiana","1913"],

["Iowa","1914"],

["Kansas","1915"],

["Kentucky","1916"],

["Louisiana","1917"],

["Maine","1918"],

["Maryland","1919"],

["Massachusetts","1920"],

["Michigan","1921"],

["Minnesota","1922"],

["Mississippi","1923"],

["Missouri","1924"],

["Montana","1925"],

["Nebraska","1926"],

["Nevada","1927"],

["New Hampshire","1928"],

["New Jersey","1929"],

["New Mexico","1930"],

["New York","1931"],

["North Carolina","1932"],

["North Dakota","1933"],

["Ohio","1934"],

["Oklahoma","1935"],

["Oregon","1936"],

["Pennsylvania","1937"],

["Rhode Island","1938"],

["South Carolina","1939"],

["South Dakota","1940"],

["Tennessee","1950"],

["Texas","1951"],

["Utah","1952"],

["Vermont","1953"],

["Virginia","1954"],

["Washington","1955"],

["West Virginia","1956"],

["Wisconsin","1957"],

["Wyoming","1958"],

["Virgin Islands","1959"] // Note: No comma on last entry
];

function getStateDate() {
var i = document.getElementById("entry").value;
if ((i < 1) || (i > USStates.length) || (isNaN(i))) { alert('Enter number from 1 to 50'); return; }
i--;
alert('In '+USStates[i][1] + " absolutely nothing happened in " + USStates[i][0] + ".");
}
</script>
</head>

<body onload="document.getElementById('entry').focus()">
<form>
<input type="text" name="entry" id="entry" value="Enter # from 1 to 50"/>
<input type="button" value="click" onclick="getStateDate()" />
</form>
</body>
</html>
[/code]

Note, I also added an input check to assure the user only enters valid numbers.
Copy linkTweet thisAlerts:
@liliputauthorJan 17.2009 — thanks alot for the help ?
×

Success!

Help @liliput 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.19,
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,
)...