Reverse decimal digits in javascript

后端 未结 14 941
灰色年华
灰色年华 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:04

    try this

    var n = 352;
    function loop(n, r){
        if(!n) return r;
        r = (r ? r * 10 : 0) + n % 10;
        return loop(Math.floor( n / 10), r);
    }
    console.log(loop(n));
    

提交回复
热议问题