0

here i have a javascript/html code. My code is supposed to read a string of one or more zipcodes from the user and return the first non-valid zipcode. It should ignore spaces. the user can only use the zipcodes 48103, 48104, 48105, 48106, 48197, 48198. anything else should be invalid. My code mostly works except when the user puts in more than one space to separate the zipcodes. so for example if the user enters "48103, space , space, space, 98324" my program should return 98324 but it just returns the space. Can anyone help? Thank you

<html>
    <head>
        <title>Due 03/30</title>

        <script>

            function test()
            {   

                var usr = prompt("Enter a string of zip codes separated by spaces: ");
                var array = [];
                array = usr.split(" ");
                //alert (array);
                var pattern = /(4810[3-6])|(4819[7-8])/;
                var str;
                var isBad = false;

                var i;
                for (i =0; i < array.length; i++)
                {    
                    str = array[i];
                    if (!str.match(pattern) && str != " ")
                       {  
                          alert ("The zipcode " + str + " is not a valid zipcode!");
                          isBad = true;
                          break;
                       }
                }

                if (isBad === false)
                  alert("All zipcodes are valid");

               }
        </script>
    </head>

    <body>

        <button onClick="test();">Test String</button>

    </body>
</html>
3
  • After splitting the string, loop through the array and remove blanks, not spaces. Commented Mar 25, 2015 at 14:55
  • 1
    It's str != "". Without the space. Commented Mar 25, 2015 at 14:56
  • Or you get rid of the consecutive white space before doing your split Commented Mar 25, 2015 at 14:56

2 Answers 2

2

Replace multiple spaces with single spaces within the split, using a regular expression:

array = usr.split(/ +/g);

(Thanks to @HBP for the edit.)

You can then replace this code:

if (!str.match(pattern) && str != " ")

… with:

if (!str.match(pattern))
Sign up to request clarification or add additional context in comments.

1 Comment

String.split can also take RegExps as parameter so : array = usr.split (/ +/) should also work
1

Your loop:

for (i =0; i < array.length; i++)
{    
    str = array[i];
    if (!str.match(pattern) && str != " ")
       {  
          alert ("The zipcode " + str + " is not a valid zipcode!");
          isBad = true;
          break;
       }
}

Should be:(str != "" not str != " ")

for (i =0; i < array.length; i++)
{    
    str = array[i];
    if (!str.match(pattern) && str != "") // No space
       {  
          alert ("The zipcode " + str + " is not a valid zipcode!");
          isBad = true;
          break;
       }
}

This is because when you split the string: "JavaScript is Awesome!" into spaces, then the javascript sees it as "JavaScript"+Space+"is"+Space+""+Space+"Awesome!".

1 Comment

While my answer simplifies the problem (somewhat), this answer fully explains why the OP's code does not work. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.