Reverse decimal digits in javascript

后端 未结 14 915
灰色年华
灰色年华 2021-02-05 16:24

How do I reverse the digits of a number using bitwise?

input:

x = 123; 

output:

x = 321; 
14条回答
  •  礼貌的吻别
    2021-02-05 17:05

    Simple and quick solution: Let's assume that you want to reverse a number 4546. You will take the reminder from each division by 10 and append it to the result until the number is > 0. And simultaneously updating the num variable by dividing it by 10.

    var x = '';
    var num = 4546;
    while(num>0){
     x = x + (num%10);
     num = parseInt(num/10);
    }
    console.log(x);
    

提交回复
热议问题