www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help Me

Hybrid View

  1. #1
    FJFJR3 Guest

    Question Help Me

    Hi I need to create a program for a friend that will prompt users for the state in which they live in. Upon entering their state, the corresponding region will be kicked out to them through an alert. Can anyone please help me out and discover why an error message keeps on occuring in line 51??? It would be greatly appreciated.




    <HTML>
    <HEAD>
    <TITLE>Regions3</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS

    function regionSelect() {

    var state = prompt("You live in" + state);
    var region1 = "New England Region";
    var region2 = "Middle Atlantic Region";
    var region3 = "Southern Region";
    var region4 = "Midwestern Region";
    var region5 = "Southwestern Region";
    var region6 = "Western Region";

    if (state = "Connecticut||Maine||Massachusetts||New Hampshire||Rhode Island||Vermont"){
    alert("You live in the " + region1);
    break;
    }
    else if(state = "Delaware||Maryland||New Jersey||New York||Pennsylvania");{
    alert("You live in the " + region2);
    break;
    }
    else if(state = "Alabama||Arkansas||Florida||Georgia||Kentucky||Louisiana||Mississippi||Missouri||North Carolina||South

    Carolina||Tennessee||Virginia||West Virginia");{
    alert("You live in the " + region3);
    break;
    }
    else if (state = "Illinois||Indiana||Iowa||Kansas||Michigan||Minnesota||Nebraska||North Dakota||Ohio||South

    Dakota||Wisconsin");{
    alert("You live in the " + region4);
    break;
    }

    else if(state = "Arizona||New Mexico||Oklahoma||Texas");{
    alert("You live in the " + region5);
    break;
    }
    else (state = "Alaska||California||Colorado||Hawaii||Idaho||Montana||Nevada||Oregon||Utah||Washington||Wyoming ");{
    alert("You live in the " + region6);
    break;
    }
    }while(state = typeof(string));

    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </SCRIPT>
    </HEAD>
    <BODY onLoad="regionSelect();">
    <H1>Select reload or refresh to find another state's region!</H1>
    </BODY>
    </HTML>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    You have a number of syntax and logic testing errors.
    1. 'break' is not an appropriate ending for an 'if' statement
    2. Your string comparisons cannot use a '=' assignment statement

    I added some logic to test if the user cannot spell a state correctly.
    I compressed your code somewhat and it can be shorten further by removing he commented sections
    that I left in to show the areas changed.

    Note, you should add some logic to assure the state is capitalized as 'Utah' would be found, but 'utah' would not!

    Code:
    <!DOCTYPE html>
    <HTML>
    <HEAD>
    <TITLE>Regions3</TITLE>
    <SCRIPT type="text/javascript">
    
    function regionSelect() {
      var state = prompt("You live in what state?");
      var region1 = "New England Region";
      var region2 = "Middle Atlantic Region";
      var region3 = "Southern Region";
      var region4 = "Midwestern Region";
      var region5 = "Southwestern Region";
      var region6 = "Western Region";
      var region = 'Unknown part of the country';
    
      if ("Connecticut,Maine,Massachusetts,New Hampshire,Rhode Island,Vermont".indexOf(state) != -1) {
        region = region1;  // alert("You live in the " + region1);  // break;
      }
      if ("Delaware,Maryland,New Jersey,New York,Pennsylvania".indexOf(state) != -1) {
        region = region2;  // alert("You live in the " + region2);  // break;
      }
      if ("Alabama,Arkansas,Florida,Georgia,Kentucky,Louisiana,Mississippi,Missouri,North Carolina,South Carolina,Tennessee,Virginia,West Virginia".indexOf(state) != -1) {
        region = region3;  // alert("You live in the " + region3);  // break;
      }
      if ("Illinois,Indiana,Iowa,Kansas,Michigan,Minnesota,Nebraska,North Dakota,Ohio,South Dakota,Wisconsin".indexOf(state) != -1) {
        region = region4;  // alert("You live in the " + region4);  // break;
      }
      if ("Arizona,New Mexico,Oklahoma,Texas".indexOf(state) != -1) {
        region = region5;  // alert("You live in the " + region5);  // break;
    }
      if ("Alaska,California,Colorado,Hawaii,Idaho,Montana,Nevada,Oregon,Utah,Washington,Wyoming".indexOf(state) != -1) {
        region = region6;  // alert("You live in the " + region6);  // break;
      }
      alert("You live in the " + region);  // break;
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <H1>Find state's region!</H1>
    <button onclick="regionSelect()"> Enter State </button>
    </BODY>
    </HTML>

  3. #3
    Join Date
    Apr 2013
    Posts
    6
    ...you noticed this thread is ~ 10 years old?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Arrow

    Quote Originally Posted by iosystems View Post
    ...you noticed this thread is ~ 10 years old?
    Nope ... ... not until I had clicked the submit button.
    Question had been posed from a PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles