Any way to condense this code?

Hi,

I’m using an if…else statement to verify a delivery zip code. I tried to use “||” between the string values to verify the number, but the code won’t run properly.
Here’s the code:

export function input43_change(event) {
//Add your code for this event here:
$w(‘#text77’).text = $w(‘#input43’).value
let badzip = $w(‘#input43’).value;
if (badzip === “10001”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10011”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10018”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10019”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10020”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10036”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10010”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10016”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10017”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10022”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10012”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10013”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10014”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10004”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10005”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10006”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10007”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10038”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10280”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10002”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10003”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10009”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10021”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10028”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10065”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10075”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10128”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10023”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “10024”) {
$w(‘#Ziptext’).hide(“fade”, fadeOptions2);
$w(‘#Zipcover’).show(“fade”, fadeOptions2);
$w(‘#ZipFAQ’).hide();
} else if (badzip === “”) {
$w(‘#Ziptext’).show();
$w(‘#Ziptext’).text = “Please enter a zip code.”;
$w(‘#Zipcover’).hide(“fade”, fadeOptions5);
} else if (badzip === “Zip”) {
$w(‘#Ziptext’).show();
$w(‘#Ziptext’).text = “Please enter a zip code.”;
$w(‘#Zipcover’).hide(“fade”, fadeOptions5);
} else {
$w(‘#Ziptext’).show();
$w(‘#Ziptext’).text = “Sorry, but we don’t deliver to that zip code yet. To see a full list of the zip codes we deliver to, click here.”;
$w(‘#Zipcover’).hide(“fade”, fadeOptions5);
$w(‘#ZipFAQ’).show();
}
console.log(badzip);
}

There is more than 1 way to do it, but why don´t you try putting all the bad zipcodes into 1 string, separated by comma´s. Then, with indexOf (JavaScript String indexOf() Method) you check if this is a bad zipcode (=if indexOff is not -1) and then only do the show/hide once, instead of doing it for every zipcode individually?

Hi Giri,

Thanks for the idea. It seems, however, that this method will allow any zip code that starts with. Are there any other methods you might know? I only want it to be for those zip codes and only when the input value = those values.

Thank you,
Anney

I’m thinking that the following should work:

export function input43_change(event) {
//Add your code for this event here: 
    $w('#text77').text = $w('#input43').value
let badzip = $w('#input43').value;
if (badzip === "10001" || badzip === "10011" || badzip === "10018" || badzip === "10019" || badzip === "10020" || badzip === "10036" || badzip === "10010" || badzip === "10016" || badzip === "10017" || badzip === "10012" || badzip === "10013" || badzip === "10014" || badzip === "10004" || (badzip === "10005" || badzip === "10006" || badzip === "10007" || badzip === "10038" || badzip === "10280" || badzip === "10002" || badzip === "10003" || badzip === "10009" || badzip === "10021" || badzip === "10028" || badzip === "10065" || badzip === "10075" || badzip === "10128" || badzip === "10023" || badzip === "10024" || ) {
        $w('#Ziptext').hide("fade", fadeOptions2);
        $w('#Zipcover').show("fade", fadeOptions2);
        $w('#ZipFAQ').hide();
    } else if (badzip === "") {
        $w('#Ziptext').show();
        $w('#Ziptext').text = "Please enter a zip code.";
        $w('#Zipcover').hide("fade", fadeOptions5);
    } else if (badzip === "Zip") {
        $w('#Ziptext').show();
        $w('#Ziptext').text = "Please enter a zip code.";
        $w('#Zipcover').hide("fade", fadeOptions5);
    } else {
        $w('#Ziptext').show();
        $w('#Ziptext').text = "Sorry, but we don't deliver to that zip code yet. To see a full list of the zip codes we deliver to, click here.";
        $w('#Zipcover').hide("fade", fadeOptions5);
        $w('#ZipFAQ').show();
    }
    console.log(badzip);
}

I would recommend, instead, creating an array of bad ZIP codes and then seeing if badzip matches one of the array elements.