SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class Final Project help

    I'm trying to do a simply array and I can't get the Capital to come up. It keeps showing as "undefined," but the State comes up fine.

    Also, why do I have to put an _ between 2 words in a state otherwise it won't recognize? Thank you for your help.

    Code JavaScript:
    <html>
     
      <!--Class: CIS 1207 Programming Logic & Design-->
      <!--Assignment: Final Project-->
     
      <head>
      <title>Final Project</title>
     
      <script type="text/javascript">
     
      function Main()
      {
      var State = new Array (50);
          State[0] = "Alabama";
          State[1] = "Alaska";
          State[2] = "Arizona";
          State[3] = "Arkansas";
          State[4] = "California";
          State[5] = "Colorado";
          State[6] = "Connecticut";
          State[7] = "Delaware";
          State[8] = "Florida";
          State[9] = "Georgia";
          State[10] = "Hawaii";
          /*State[11] = "Idaho";
          State[12] = "Illinois";
          State[13] = "Indiana";
          State[14] = "Iowa";
          State[15] = "Kansas";
          State[16] = "Kentucky";
          State[17] = "Louisiana";
          State[18] = "Maine";
          State[19] = "Maryland";
          State[20] = "Massachusetts";
          State[21] = "Michigan";
          State[22] = "Minnesota";
          State[23] = "Mississippi";
          State[24] = "Missouri";
          State[25] = "Montana";
          State[26] = "Nebraska";
          State[27] = "Nevada";
          State[28] = "New Hampshire";
          State[29] = "New Jersey";
          State[30] = "New Mexico";
          State[31] = "New York";
          State[32] = "North Carolina";
          State[33] = "North Dakota";
          State[34] = "Ohio";
          State[35] = "Oklahoma";
          State[36] = "Oregon";
          State[37] = "Pennsylvania";
          State[38] = "Rhode Island";
          State[39] = "South Carolina";
          State[40] = "South Dakota";
          State[41] = "Tennessee";
          State[42] = "Texas";
          State[43] = "Utah";
          State[44] = "Vermont";
          State[45] = "Virginia";
          State[46] = "Washington";
          State[47] = "West Virginia";
          State[48] = "Wisconsin";
          State[49] = "Wyoming";*/
     
      var Capital = new Array (50);
          Capital[0] = "Montgomery";
          Capital[1] = "Juneau";
          Capital[2] = "Phoenix";
          Capital[3] = "Little_Rock";
          Capital[4] = "Sacramento";
          Capital[5] = "Denver";
          Capital[6] = "Hartford";
          Capital[7] = "Dover";
          Capital[8] = "Tallahassee";
          Capital[9] = "Atlanta";
          Capital[10] = "Honolulu";
          /*Capital[11] = Boise;
          Capital[12] = Springfield;
          Capital[13] = Indianapolis;
          Capital[14] = Des_Moines;
          Capital[15] = Topeka;
          Capital[16] = Frankfort;
          Capital[17] = Baton_Rouge;
          Capital[18] = Augusta;
          Capital[19] = Annapolis;
          Capital[20] = Boston;
          Capital[21] = Lansing;
          Capital[22] = St._Paul;
          Capital[23] = Jackson;
          Capital[24] = Jefferson_City;
          Capital[25] = Helena;
          Capital[26] = Lincoln;
          Capital[27] = Carson_City;
          Capital[28] = Concord;
          Capital[29] = Trenton;
          Capital[30] = Santa_Fe;
          Capital[31] = Albany;
          Capital[32] = Raleigh;
          Capital[33] = Bismarck;
          Capital[34] = Columbus;
          Capital[35] = Oklahoma_City;
          Capital[36] = Salem;
          Capital[37] = Harrisburg;
          Capital[38] = Providence;
          Capital[39] = Columbia;
          Capital[40] = Pierre;
          Capital[41] = Nashville;
          Capital[42] = Austin;
          Capital[43] = Salt_Lake_City;
          Capital[44] = Montpelier;
          Capital[45] = Richmond;
          Capital[46] = Olympia;
          Capital[47] = Charleston;
          Capital[48] = Madison;
          Capital[49] = Cheyenne;*/
     
        //input section
        Choice=(document.frmMain.txtChoice.value);
        //Choice="California", x="CALIFORNIA"
        x=Choice.toUpperCase();
     
        //output section
        var City=0, J;
     
        for (J=0;J<50;J++)
        {
        //y=State[J].toUpperCase();
        //if (y==x)
        {
        City=Capital[J]; 
        }   
     
        }
        if (City !=0)
        {
        document.frmMain.taOutput.value="State: "+Choice+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"Capital: "+City+"\n";
        }
     
        else
        {
        alert("State: "+Choice+"\n" + "Wrong Spelling");
     
        return;
        }
     
        function ClearData()
        {
        document.frmMain.taOutput.value="";  
        }   
      } 
     
      </script>
      </head>
     
      <body bgcolor="lightblue">
        <u><h2 style="text-align:center">United States Capitol Date</h2></u>
     
      <form name="frmMain" style="text-align:center">
      <br />
      Enter the State name: <input type="text" name="txtChoice" size=25 value="" onClick="ClearData();"/>
      <br /><br />
      <input type="button" value="Click for Entry Date" onClick="Main()"/>
      <br /><br />
      <textarea name="taOutput" rows=5 cols=30 wrap="virtual"></textarea>
     
      </form>
      </body>
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Run your scripting code through this online lint and you will find the most likely cause of your problems.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •