Write a function rev that turns an integer upside down.

For example, rev(12345) will return 54321, and rev(123) will return 321. There is no limit to the number of integer digits.

function sum(a){

        var s = 0;

    while(a){// The first time 123 The second time 12 The third time 1 The fourth time 0 stops the loop

        s=s*10 +a%10;//    3          30+2        320+1

        a=parseInt(a/10);//12           1           0

    }

    return s;

    }

 

    console.log(sum(123));

 

The second method

 

function wei(a){

        //用for

        for (var i = 1; true; i ++) {

            if(parseInt(a/Math.pow(10,i)) < 1){

                return i;

            }

        }

        //用while

        // var i = 0;

        // while(true){

        //  i++;

        //  if(a/Math.pow(10,i)<1){

        //  // console.log(i);

        //      return i;

        //  }

        // }

    }

    function rev(b){

            var n = wei(b);

            var tol = 0;

        for(var i=1;i<=n;i++){

            tol +=parseInt(b/Math.pow(10,i-1))%10*(Math.pow(10,n-i));

        }

        alert(b+"'s inverse"+tol);

    }

    var c = parseInt(prompt());

 

    rev(c);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326918475&siteId=291194637