Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda