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

113
Views
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 answers
Answer question

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 Report

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