Search code examples
javascriptfunctionif-statementletter

This code is supposed to reject pin codes that aren't 4 digits or 6, and its supposed to reject codes that have letters in them


The code rejects pins with letters which it's supposed to do, but it rejects valid pins. Below is the code can you see where I went wrong.

function validatePIN (pin) {
    let letters = /^[A-Za-z]+$/;
    if (pin.match(letters)) {
      return false;
      if (pin.length = 4 || 6) 
      return true;
    } 
    return false;
}

Solution

  • You have typo in checking the value, your if is missing a } and your regex is checking if there is any letter at the start and finish of the pin, it should check if it has any letters

    function validatePIN (pin) {
    let letters = /[A-Za-z]/;
      if (pin.match(letters)) {
        return false;
      }
    
      if (pin.length === 4 || pin.length === 6) {
        return true;
      } 
      return false;
    }