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

254
Visualizações
The only word that is not passing the test is "almostomla."

I am learning JS on freecodecamp and am stuck on a problem. I am creating a palindrome checker. My code almost works. However, there is only one word that is not passing the test and that is almostomla. It is not a palindrome yet my code returns true. I have tried several things. Even rewrote the code and used the while loop but nothing seems to help. There is a solution at the freeCodecamp web but I have written the code in a different way and am unable to figure my mistake out.

Here is my code.

let reversedStr = [];

function palindrome(str) {
  let d = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
  for (let i = d.length - 1; i >= 0; i--) {
    reversedStr.push(d[i]);
  }
  for (let j = 0; j < str.length; j++) {
    if (reversedStr[j] == d[j]) {
      return true;
    } else {
      return false;
    }
  }
}
console.log(palindrome("almostomla"));

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

0

This line if (reversedStr[j] == d[j]) {return true; returns as soon as the character matches at both the index. It does not check rest of the characters.

In fact you can just return as soon as the character in both index does not match.

Also note the reversedStr has to be inside the function. Else it will contain previous values

function palindrome(str) {
  let reversedStr = [];
  let d = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
  for (let i = d.length - 1; i >= 0; i--) {
    reversedStr.push(d[i]);
  }
  
  for (let j = 0; j < str.length; j++) {
    if (reversedStr[j] !== d[j]) {
      return false;
    }
  }
  return true
}
console.log(palindrome("almostomla"));
console.log(palindrome("1221"));

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code returns "true" or "false" after first match - it checks that 'a' is equal to 'a' and returns true.

You can return "false" from inside of cycle only if you found duplicated letter else return true after the cycle ends.

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