Jump to content

[SOLVED] Listing States by Population


twilitegxa

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.