1

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)

3
  • while (x > 0) will not really work well if x is negative. (note that the reverse of a negative x is simply -reverse(-x)) Commented Apr 16, 2022 at 7:40
  • 1
    if you didn't want to change your logic you could use abs on top of your input and deal with the negative sign at the end Commented Apr 16, 2022 at 7:43
  • 1
    convert negative integer to positive by multiplying with -1, then reverse then convert it back to negative. Commented Apr 16, 2022 at 7:44

5 Answers 5

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

0

parse to string -> reverse -> parse to number back

function reverse(num){
  return num < 0 ? 
    ((num * -1)+'').split('').reverse().join('') *-1 
    : (num+'').split('').reverse().join('') *1
}

Comments

0
var negativeNumber = -9813;

function convertToPositive(number) {
  if (number < 0) {
    var positiveNum = Math.abs(number);
    var stringNum = positiveNum.toString();
    var numArray = stringNum.split('');
    var reverseNum = numArray.reverse();
    var reverseString = reverseNum.join('');
    var reverseNum = parseInt(reverseString, 10);
    var negativeReverseNum = reverseNum * -1;
    return negativeReverseNum;
  }
}

convertToPositive(negativeNumber);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.