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

225
Visualizações
Finding Palindrome Number with js but problem with negative numbers
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    
  let a, b, c = 0;
    a = x%10;
    x=x-a;
    b= x/10;
    c= b%10;
    let d = b-c;
    let f = d/10;
    let g = (a*100) + (c*10) + f;
    if (g==x && x>0)
        return "true";
    else 
        return "false";
};

when the number is negative its not saying false, it says true instead. But i added x>0 condition. What's the problem? And btw my code also doesn't look good maybe it's not a good way to solve the palindrome problem. And also i am not so sure if its working with numbers with more than 3 digits.

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

0

I suppose that you want to use mathematical operations to get the answers as divisions and modules. You can use the following code that works for any number:

/**
* @param {number} x
 * @return {boolean}
 */
var isPalindrome = function (x) {
  if (x < 0) {
    return false;
  }
  const original = x;
  let pali = 0;
  while (x > 0) {
    pali = (x % 10) + pali * 10;
    x = Math.floor(x / 10);
  }
  return original === pali;
};

Also the conditional evaluation in your code is not in the correct place, the first thing that you need to do is evaluate if the number is a positive one, if the number is negative you don't have to do any processing because you know that the number is not a palindrome.

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