Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
LeetCode javascript Palindrome HECHO pero sigue obteniendo un resultado incorrecto
/** * @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 ; };

Sigo obteniendo un resultado incorrecto como falso, pero mi lógica es correcta. Esta es la pregunta del palíndromo de leetcode. Lo estoy intentando con la lógica de JavaScript, es correcta pero aún no puedo resolverlo.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Su lógica parece estar bien, pero la ejecución tiene algunos errores menores. Utilice el siguiente fragmento, debería funcionar:

 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 Report

0

Solo hay un problema:

En JavaScript, los números son números de punto flotante por defecto, por lo que / realiza una división de punto flotante. Necesitas truncar eso:

 x = Math.floor(x / 10);

Una observación sobre su código:

La construcción if (boolean) return true; else false de if (boolean) return true; else false es un antipatrón. Dado que boolean ya representa el valor que desea devolver, solo debe devolverlo. Así que en tu caso haz:

 return num == y;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!