twilitegxa Posted August 5, 2009 Share Posted August 5, 2009 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2009 Share Posted August 5, 2009 Where is "rank" defined? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 5, 2009 Author Share Posted August 5, 2009 var rank = i+1; But I see I have i as: var i; That's not really doing anything, is it? Do I need to use an array or is there another way I can do this? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2009 Share Posted August 5, 2009 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> Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 5, 2009 Author Share Posted August 5, 2009 Thank you! That seems to have done the trick! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.