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

240
Views
How to reverse a negative integer in javascript?

So I came across a problem "How to reverse an integer in javascript?" I successfully managed to reverse the positive numbers for eg if I enter 123 then I get the output 321 but on the hand, if I am trying some negative number like -123 then I get 0 as the output. How can I solve this issue and get output as -321?

var reverse = function(x){
    let a = 0;
    while(x>0){
        a = a * 10 + x%10; 
        // 0 = 0 *10 + 123%10 = 3
        // a=3
        // 0 = 0 *10 + 12%10 = 2
        // a=2
        // 0 = 0*10 + 1%10 = 1
        // a=1
        x = Math.floor(x/10)
         //  x = 123/10 = 12
        //  x = 12/10 =1
    }
    console.log(a);
    return a;
}
var x = -123;
reverse(x)

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

0

Just Preserve the sign of an integer, like

var reverse = function(x){
    let sign = x<0?-1:1;
    let a = 0;
    x=Math.abs(x);
    while(x>0){
        a = a * 10 + x%10; 
        // 0 = 0 *10 + 123%10 = 3
        // a=3
        // 0 = 0 *10 + 12%10 = 2
        // a=2
        // 0 = 0*10 + 1%10 = 1
        // a=1
        x = Math.floor(x/10)
         //  x = 123/10 = 12
        //  x = 12/10 =1
    }
    console.log(sign*a);
    return a;
}
var x = -123;
reverse(x)
about 4 years ago · Juan Pablo Isaza Report

0

Convert negative integer to positive by multiplying with -1, then reverse then convert it back to negative

var reverse = function(x){
    let isNegative=x<0;
    x=Math.abs(x);
    let a = 0;
    while(x>0){
        a = a * 10 + x%10; 
        // 0 = 0 *10 + 123%10 = 3
        // a=3
        // 0 = 0 *10 + 12%10 = 2
        // a=2
        // 0 = 0*10 + 1%10 = 1
        // a=1
        x = Math.floor(x/10)
         //  x = 123/10 = 12
        //  x = 12/10 =1
    }
    console.log(a);
    return isNegative ? a* -1 : a;
}
about 4 years ago · Juan Pablo Isaza Report

0

let reverse = function(x){
    let a = 0;
    while(true){
        a = a * 10 + x%10; 
        // 0 = 0 *10 + 123%10 = 3
        // a=3
        // 0 = 0 *10 + 12%10 = 2
        // a=2
        // 0 = 0*10 + 1%10 = 1
        // a=1
        x = x > 0 ? Math.floor(x/10) : Math.ceil(x/10);
        //  x = 123/10 = 12
        //  x = 12/10 =1
      if(x === 0){
        break;
      }
    }
    console.log(a);
    return a;
}
let x = -123;
reverse(x);

Explanation :

  1. Changed while loop condition from x>0 to true to enable entering into loop when the argument is less than or equal to zero
  2. Math.floor() round the number to the nearest smaller integer. Which means

Math.floor(10.5); Console output : 10

Math.floor(-10.5); Console output : -11

Incorporated the logic using Math.ceil() function when the number is less than zero.

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!