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

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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