Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda