Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda