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

108
Vistas
LeetCode javascript Palindrome DONE but still getting wrong output
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    var y=x,num=0,rem;
    while(x>0)
        {
            rem=x%10;
            num=(num*10)+rem;
            x=x/10;
        }
    if(num==y)
        return true;
    else
        return false ;
};

I am still getting wrong output as false but my logic is correct. This is leetcode palindrome question i am trying it with javascript logic is correct but still not able to figure it out.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your logic seems fine but execution is having some minor errors. Please use below snippet, it should work:

var isPalindrome = function(x) {
    if (x < 0) {
        return false;
    }
    // Store the number in a variable
    let number = x;
    // This will store the reverse of the number
    let reverse = 0;
    while (number > 0) {
        reverse = reverse * 10 + number % 10;
        number = parseInt(number / 10);
    }
    return x === reverse;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

There is just one issue:

In JavaScript numbers are floating point numbers by default, and so / performs a floating point division. You need to truncate that:

    x = Math.floor(x / 10);

A remark on your code:

The construct if (boolean) return true; else false is an anti-pattern. Since boolean already represents the value you want to return, you should just return it. So in your case do:

return num == y;
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