Jump to content

Recommended Posts

What am I doing wrong in this script?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>States By Population</title>
<script type="text/javascript">

var states = new Array("California", "Texas", "New York", "Florida", "Illinois", "Pennsylvania", "Ohio", "Michigan", "Georgia", "North Carolina", "New Jersey", "Virginia", "Massachusetts", "Washington", "Indiana", "Arizona", "Tennessee", "Missouri", "Maryland", "Wisconsin", "Minnesota", "Colorado", "Alabama", "South Carolina", "Louisiana", "Kentucky", "Oregon", "Oklahoma", "Connecticut", "Iowa", "Mississippi", "Arkansas", "Kansas", "Utah", "Nevada", "New Mexico", "West Virginia", "Nebraska", "Idaho", "Maine", "New Hampshire", "Hawaii","Rhode Island", "Montana", "Delaware", "South Dakota","Alaska","North Dakota","Vermont","Wyoming");
var i;	
</script>
</head>

<body>
<h3>US States by Population</h3>

<script type="text/javascript">
for (i=0;i<rank.length;i++){
	var rank = i+1;
	if (rank>10){
		rank = "0"+rank;
	}
	document.write("Rank: " + rank + "<p>" + states[rank] + "<br />");
}

</script>

 

It is supposed to list the states according to their population, but none of them are showing up. I know I have done something wrong, but I can't figure out what. Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/168997-solved-listing-states-by-population/
Share on other sites

Well, the problem is that you are using the length of rank for your loop before it is ever defined:

for (i=0;i<rank.length;i++){

 

Since it has no value that loop never runs. you need to use the length of the states array! Also, your comparison to add a 0 to the rank is backwards - I'm assuming you want the value less than 10 to have a leading zero, correct? Although there is more I would change, this will work:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>States By Population</title>

<script type="text/javascript">

var states = new Array(
                        "California", "Texas", "New York", "Florida", "Illinois", "Pennsylvania", "Ohio",
                        "Michigan", "Georgia", "North Carolina", "New Jersey", "Virginia", "Massachusetts",
                        "Washington", "Indiana", "Arizona", "Tennessee", "Missouri", "Maryland", "Wisconsin",
                        "Minnesota", "Colorado", "Alabama", "South Carolina", "Louisiana", "Kentucky",
                        "Oregon", "Oklahoma", "Connecticut", "Iowa", "Mississippi", "Arkansas", "Kansas",
                        "Utah", "Nevada", "New Mexico", "West Virginia", "Nebraska", "Idaho", "Maine",
                        "New Hampshire", "Hawaii","Rhode Island", "Montana", "Delaware", "South Dakota",
                        "Alaska","North Dakota","Vermont","Wyoming"
                    );

</script>

</head>

<body>
<h3>US States by Population</h3>

<script type="text/javascript">
var stateLen = states.length;
var rank;
for (var i=0; i<stateLen; i++){
	rank = i+1;
	if (rank<10){
		rank = "0"+rank;
	}
	document.write("<p>Rank: " + rank + "<br />" + states[i] + "</p>");
}

</script>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.