How to check a postal code in JavaScript?

I am trying to find out if this person needs to pay for shipping costs.

What I did is divide the postal code into numbers and letters. Next, I check to see if the input is between 1000 and 2000 and between AA en BB.

Problem: When I type in postal code 1000AA or 2000BB or something in between I always get the else answer even when the if statement is correct.

var sendingCost = 15;
var city = prompt("What city do you live in?");
var postalCode = prompt("What is your postal code?");
var postalCodeC = postalCode.slice(0, 3);
var postalCodeL = postalCode.slice(4, 5);

if (city == 'Amsterdam' && postalCodeC >= 1000 && postalCodeC <= 2000 && postalCodeL >= 'AA' && postalCodeL <= 'BB') {
  alert('There is no sending cost')
} else {
  alert('The sending cost is €15.')
};

3 answers

  • answered 2018-10-11 19:56 stalkert

    Try this

    var postcodec = +postcode.slice(0, 4); var postcodeL = postcode.slice(4, 6);

  • answered 2018-10-11 20:00 John Krakov

    As @Ivar mentioned, I don't think you understand how works the slice function. The first argument should be the the begin position and the second should be the end position. Thus, if you want to select only the first 4 numbers, and then the 2 letters you should use :

    let postcode = "1500BD";
    //Also, simply using slice will return a string, thus, you may want to convert it using Number();
    let num = Number(postcode.slice(0, 4));
    let letters = postcode.slice(4);
    

  • answered 2018-10-12 00:44 Roy Scheffers

    Your slice() is not taking all four numbers of the postal code. Instead, use the following postalCode.slice(0, 4).

    Have a look at the Mozilla docs regarding slice.

    In the working code snippet below also note the following three lines.

    var postalCodeC = Number(postalCode.slice(0, 4));
    // converts the alphanumeric value from prompt to a number for better comparison.
    var postalCodeL = postalCode.slice(-2).toUpperCase();
    // converts the letters of the postal code to CAPS, this way Aa, AA or aa will be valid too.
    var correctCity = city.toLowerCase() === 'amsterdam';
    // the same here, convert city to lowercase letters and compare the input to 'amsterdam'
    

    Working example.

    var sendingCost = 15;
    var city = prompt("What city do you live in?");
    var postalCode = prompt("What is your postal code?");
    var postalCodeC = Number(postalCode.slice(0, 4));
    var postalCodeL = postalCode.slice(-2).toUpperCase();
    
    var correctCity = city.toLowerCase() === 'amsterdam';
    var withinPostalArea = postalCodeC >= 1000 && postalCodeC <= 2000 && postalCodeL >= 'AA' && postalCodeL <= 'BB';
    
    console.log(postalCodeC);
    console.log(postalCodeL);
    
    if (correctCity && withinPostalArea) {
      alert('There is no sending cost');
    } else {
      alert('The sending cost is €' + sendingCost);
    };

    NOTE: In order to help you debug these issues. console.log() the output to check the value of the variable and see if it is what you expect it to be.