0

I have a function for verifying and possibly modifying postal codes from a test file. It verifies correct string length, that there's a space in the middle of the 6 characters (& if not, to insert one), etc. My regExp test is working, but I'm having trouble inserting a space in the middle of a string.

function fixPostalCode(postalCode) {
    var invalidChars =/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$/i;
    postalCode = postalCode.toString().trim();
   
    if (postalCode.length = 6 && invalidChars.test(postalCode.toString())) {
        return postalCode.toUpperCase();
    }

    if (postalCode.length = 5 && postalCode.charAt(3) !== ' ' && invalidChars.test(postalCode.toString())) {
        return postalCode.slice(0, 3) + " " + postalCode.slice(3, 6);

    } else {
        throw 'Invalid postal code';
    }
}

The test I'm having trouble with is this:

  test('an internal space is added', function () {
    const postalCode = 'A1A1A1';
    expect(fixPostalCode(postalCode)).toEqual('A1A 1A1');
  });

my slice method isn't doing anything to the string.

4
  • 2
    How do you determine the methods do not work? What are sample inputs for this function - what is the intended result and what do you get instead? Commented May 30, 2022 at 20:43
  • I'm using Jest to run a series of tests, for instance: ``` test('trailing space is removed', function () { const postalCode = 'A1A 1A1 '; expect(fixPostalCode(postalCode)).toEqual('A1A 1A1'); }); ``` It tests each of the aforementioned conditioned and gives a pass or fail. My code is throwing at this test, and for trailing/leading spaces being removed, as well as the space not being added. Commented May 30, 2022 at 21:04
  • Your function is returning postalCode but what you are expecting is the result of str. I tried your functions with some values and is always returning return postalCode.toUpperCase(). Change the value to str if this is what you are expecting to be. Commented May 30, 2022 at 21:08
  • The code throws alright, but not because the trim() fails. It's because the function doesn't work in general - the regex is wrong. So, when it fails the throw clause is reached. Commented May 30, 2022 at 21:09

2 Answers 2

2

trim() removes whitespace from both sides of a string not from the middle of string. As I see from your description you are trying to cancel whitespace in the middle of the string which is not possible with trim. You should use replace

function fixPostalCode(postalCode) {
    let test1 = postalCode.toString().trim();
    console.log(test1) ///It will fail
    let test2 = postalCode.replace(/ +/g, "");
    console.log(test2) ///It will be succesfull
}

fixPostalCode('A1A 1A1');

Sign up to request clarification or add additional context in comments.

Comments

0

You're most of the way there, but the order of your space insertion wasn't being picked up by the later Regex as valid. Without changing the regex, your code is functional by swapping the order of your toUpperCase method with the space insertion via slice:

function fixPostalCode(postalCode) {
  var invalidChars = new RegExp(/([ABCEGHJKLMNPRSTVXY]\d)([ABCEGHJKLMNPRSTVWXYZ]\d){2}/i);
  postalCode = postalCode.toString().trim();

  if (invalidChars.test(postalCode.toString())) {
    postalCode = postalCode.toUpperCase();
  }

  if (postalCode.charAt(3) !== ' ') {
    return postalCode.slice(0, 3) + ' ' + postalCode.slice(3, 6);
  } else {
    throw 'Invalid postal code';
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.