Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
Regex validate PIN Codewars

For this Codewars challenge, I cannot understand why the logic is not working. I've found other solutions, but would like to understand why this is not working:

ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.

If the function is passed a valid PIN string, return true, else return false.

function validatePIN (pin) {
  
  const splitPin = pin.toString().split('')
  const finalArr = [];
  
for (let i = 0; i < splitPin.length; i++){
  
  if (!Number.isInteger(pin[i])){
    return false;
  } else if (Number.isInteger(pin[i])){
    finalArr.push(pin[i]);
  }
}
  if(finalArr.length === 4 || finalArr.length === 6){
    console.log(finalArr.length)
    return true;
  } 
  
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are some double checks that you don't need to do but you are doing because you're not sure what you're expecting.

Like the type for pin.

const splitPin = pin.toString().split('') works either way for a string or number value, because you are expecting a string, the call to .toString is unneccesary

But also that you're using Number.isInteger - this function will only return true if the parameter is of type integer.

But since pin is a string and splitting it returns an array of strings, the Number.isInteger call will always return false.

Try using isNaN instead. Then check for !isNaN(pin[0]) (Thanks to cyberbrain).

You can streamline the function from:

function validatePIN (pin) {
  
  const splitPin = pin.toString().split('')
  const finalArr = [];
  
for (let i = 0; i < splitPin.length; i++){
  
  if (!Number.isInteger(pin[i])){
    return false;
  } else if (Number.isInteger(pin[i])){
    finalArr.push(pin[i]);
  }
}
  if(finalArr.length === 4 || finalArr.length === 6){
    console.log(finalArr.length)
    return true;
  } 
  
}

to something cleaner:

function validatePIN (pin) {
  const acceptedLengths = [4, 6]
  const splitPin = pin.toString().split('')
  const finalArr = [];
  
  for (let i = 0; i < splitPin.length; i++){
    // You don't need to do an else, since you're only
    // checking for one condition.
    // just return directly
    if (!isNaN(pin[i])){
      finalArr.push(pin[i]);
    }

    return
  }
  
  return acceptedLengths.includes(finalArr.length)
}

or even something like this:

function validatePIN(pin) {
  // Set a variable for the accepted length
  const acceptedPINLength = [4, 6];

  // If we have to check if a value in pin is an integer or not
  // then the pin variable is a string and can't be a number.
  // So skip the .toString and just split it directly.
  const numbersArray = pin.split('')
    .filter((entry) => !isNaN(entry))

  // if strength is 4 or 6, return true, else false
  return acceptedPINLength.includes(numbersArray.length)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I would say: no loop required at all, you don't have to filter any characters:

function validatePIN(pin) {
  const acceptedPINLength = [4, 6];
  return acceptedPINLength.includes(pin.length) && !isNaN(pin);
}

(Thanks to @Amats for the elegant solution with the acceptedPINLength.)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda