1

I can't seem to get this code to work (namely, getting isInArray(userZipCode,chicagoZipCodes) to return true.

  var chicagoZipCodes = [ 60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609,
                          60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620, 60621,
                          60622, 60623, 60624, 60625, 60626, 60628, 60630, 60631, 60632, 60634, 60636, 60637,
                          60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 60647, 60649, 60651, 60652,
                          60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60706, 60707, 60714 ]

  function isInArray(value, array) {
    return array.indexOf(value) !== -1
  }

  elem.bind('change', function(e) {
    userZipCode = $('#zipCode').val();

    alert(isInArray(userZipCode,chicagoZipCodes));

    if ( !(isInArray(userZipCode, chicagoZipCodes)) && (userZipCode.length > 4) ) {
      // success
      alert("Sorry, that is not a Chicago area zip code! :(");
      e.stopImmediatePropagation();
    }
  })
3
  • Thanks for all the quick answers guys! Looks like parseInt was what I was looking for -- quick and simple solution Commented Jun 20, 2015 at 14:38
  • Just note that care should be taken when using parseInt: stackoverflow.com/questions/4090518/… Commented Jun 20, 2015 at 15:22
  • Thanks for the heads up Jason Commented Jun 20, 2015 at 15:24

3 Answers 3

4

Look like you compare numbers and strings

function isInArray(value, array) { 
        return array.indexOf(parseInt(value,10)) !== -1 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

To add context to this answer, I edited the question to include return in isInArray but the answer was adding parseInt
1

Parse your $('#zipCode').val() before using the indexOf() function.

return array.indexOf(parseInt(value);

Comments

1

All values returned from the DOM are strings so you need to convert them to a number. You can do this a few ways but the most direct is to use Number as a function rather than a constructor.

 var chicagoZipCodes = [60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609,
   60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620, 60621,
   60622, 60623, 60624, 60625, 60626, 60628, 60630, 60631, 60632, 60634, 60636, 60637,
   60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 60647, 60649, 60651, 60652,
   60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60706, 60707, 60714
 ]

 function isInArray(value, array) {
   return array.indexOf(value) !== -1
 }

 elem.bind('change', function(e) {
   // All values returned from the DOM are strings
   // so we need to convert them to a number
   userZipCode = Number($('#zipCode').val());

   alert(isInArray(userZipCode, chicagoZipCodes));

   // Check length first since all Chicago zips listed are greater than 4 digits
   // Saves the more expensive check of searching the array for
   //   only zips that are greater than 4
   if ((userZipCode.length > 4) && !(isInArray(userZipCode, chicagoZipCodes))) {
     // success
     alert("Sorry, that is not a Chicago area zip code! :(");
     e.stopImmediatePropagation();
   }
 });

Comments

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.