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

272
Visualizações
telephone number check is returning false when it should return true and I cant find where it messes up

I am completing a telephone checker project and I am able to correctly identify the false results but the true ones are not returning true.

Here is my code so far. I have checked the individual functions and they all seem to work, although I have no knowledge of how to check for errors besides that. Any suggestions are greatly appreciated!

function telephoneCheck(str) {
  let newStr = str;
  let numberOfNumbers = 0;
  if (newStr.match(/[!^A-Za-z!@#$%^&*_+=<>,.:]/gm) == []) {
    for (let i = 0; i < newStr.length; i++) {
      if (!isNaN(newStr[i])) {
        numberOfNumbers += 1;
      }
    }
    if (numberOfNumbers == 10) {
      return true;
    } else if (newStr[0] == 1) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}
console.log(telephoneCheck("555-555-5555"));

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Many issues. You cannot compare an array to an array, they are never the same

Here is what I think you wanted

const notNumDash= /[^\-0-9]/g; // something other than numbers and dashes 
const telephoneCheck = str => {
  if (notNumDash.test(str)) return false; 
  const numberOfNumbers = str.replace(/[^0-9]/g, ''); // remove all not numbers
  return numberOfNumbers.length === 10 || numberOfNumbers[0] == 1; // either or returns true, all others false
}
console.log("555-555-5555",telephoneCheck("555-555-5555"));
console.log("5XXX55-555-5555",telephoneCheck("5XXX55-555-5555"));

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