-1

So, I'm writing code to search for zip codes entered by the user , the code will then state what the County is, if it's valid or state that the county is invalid (within one state). Funny thing is, it works , one the first line only, the second line isn't being processed and I'm not sure what I'm doing wrong. I checked online and I seem to match the examples I'm seeing. I will show the code below:

function boroughCheck () {
var n = prompt("Enter a zip code", " ");
var entered = "Borough located:";

if( n == "10451"||"10452"||"10453"||"10454"||"10455"||"10456"||"10457"||"10458"||"10459"||"10460"||"10461"||"10462"||"10463"||"10464"||"10465"||"10466"||"10467"||"10468"||"10469"||"10470"||"10471"){
   
  alert(entered + " Bronx ");
}
else if( n == "11201"||"11202"||"11203"||"11204"||"11205"||"11206"||"11207"||"11208"||"11209"||"11210"||"11211"||"11212"||"11213"||"11214"||"11215"||"11216"||"11217"||"11218"||"11219"||"11220"||"11221"||"11222"||"11223"||"11224"||"11225"||"11226"|"11227"||"11228"||"11229"||"11230"||"11231"||"11232"|"11233"||"11234"||"11235"|"11236"||"11237"||"11238"||"11239"||"11241"||"11242"||"11243"||"11245"||"11247"||"11249"||"11251"||"11252"||"11256"){

  alert(entered + " Brooklyn ");
}
else
{
  alert(" - Invalid Zip Code")}
}

boroughCheck();

So, what am I doing wrong in this code that makes the first "if" statement run, but not the second "else if " statement . Any zip code I enter is being shown as "Bronx" even if it matches what I've entered in for the Brooklyn area ? Any ideas ?

KoshVorlon
  • 144
  • 8
  • Most likely because every other zip code is not being compared. You can't do `n == "zipcode1" || "zipcode2"` Because zipcode 2 is not being compared – RTarson Nov 09 '18 at 15:58
  • 1
    you'd have to do n== ''123" || n == "124" but thats not the best way to approach this problem – Joe Warner Nov 09 '18 at 15:58
  • who voted to close this as too broad? lol.... should be a dupe. Just have to find it – epascarello Nov 09 '18 at 16:01

1 Answers1

2

JavaScript is written wrong.

You need to do if (n == "10451" || n ==... ) and so on.

You could also avoid that by writing two arrays, one for bronx and one for brooklyn, then testing this way:

var bronx = ["10451","10452"];
var brooklyn = ["11201","11202"];
var n = prompt("Enter a zip code", " ");
var entered = "Borough located:";

if(bronx.indexOf(x) === true){
    alert(entered + " Bronx ");
}elseif(brooklyn.indexOf(x) === true){
    alert(entered + " Brooklyn ");
}else{
    alert(" - Invalid Zip Code")}
}

method 3, use a range. If those zipcodes won't change and they're consistent,

if (n >= 11201 && n <= 11256) {
  // bronx
}elseif ( n >= 10451 && n <= 10471 ) {
  // brooklyn
}else{
  // nope
}
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • To add-on to this good answer, and answer your *"Why does the first `if` run but not the `else`?"*: `n == "10451" || "10452" || "10453"` will always return `true`. Any non-empty string in JavaScript is considered `truthy`, so it's being evaluated as `if (n == "10451" || true || true || true)` etc. – Tyler Roper Nov 09 '18 at 16:08