I am trying to convert the following if, else if, else to a switch statement.
if(zipValue === "") {
setErrorFor(zip, 'Please enter a zip code');
} else if (!isZip(zipValue)) {
setErrorFor(zip, 'Not a valid Aust zip code');
} else {
setSuccessFor(zip);
}
So far I have tried:
switch (zipValue) {
case "":
setErrorFor(zip, 'Please enter a zip code');
break;
case !isZip(zipValue): //can't get this working
setErrorFor(zip, 'Not a valid Aust zip code');
break;
default:
setSuccessFor(zip);
break;
}
the function isZip checks against a regex as shown here:
function isZip(zip) {
return /^[0-9]{4}$/.test(zip);
}
The original if, else if, else is working I just want to learn more about using a switch statement and make my code cleaner. Thank you.
PS after posting this I now see the irony that the switch statement is using more lines of code.