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

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

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