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

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

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