[removed] How to reverse a number?

前端 未结 10 885
余生分开走
余生分开走 2020-11-29 11:54

Below is my source code to reverse (as in mirror) the given number. I need to reverse the number using reverse method of arrays.

var a = prompt(\"Enter a va         


        
相关标签:
10条回答
  • 2020-11-29 12:06

    Firstly, I don't think you are using an array to store the number. You are using a java script variable.

    Try out this code and see if it works.

    var a = prompt("Enter a value");
    var z = a;
    var reverse = 0;
    while(z > 0)
    {
        var digit = z % 10;
        reverse = (reverse * 10) + digit;
        z = parseInt(z / 10);
    }
    alert("reverse = " + reverse);
    
    0 讨论(0)
  • 2020-11-29 12:06

    Explanation

    Using the JavaScript reverse() array method you can reverse the order of the array elements.

    Code

    var a = prompt("Enter a value");
    var arr = [];
    
    for (var i = 0; i < a.length; i++) {
        arr[i] = a.charAt(i);
    }
    arr.reverse();
    alert(arr);

    0 讨论(0)
  • 2020-11-29 12:08

    Assuming you may want to reverse it as a true number and not a string try the following:

    function reverseNumber(num){
        num = num + '';
            let reversedText = num.split('').reverse().join('');
            let reversedNumber = parseInt(reversedText, 10);
        console.log("reversed number: ", reversedNumber);
            return reversedNumber;
      }
    
    0 讨论(0)
  • 2020-11-29 12:10

    Keeping the type number, without any high-order function:

    function flipInt(n){
        var digit, result = 0
    
        while( n ){
            digit = n % 10  //  Get right-most digit. Ex. 123/10 → 12.3 → 3
            result = (result * 10) + digit  //  Ex. 123 → 1230 + 4 → 1234
            n = n/10|0  //  Remove right-most digit. Ex. 123 → 12.3 → 12
        }  
      
        return result
    }
    
    
    // Usage: 
    alert(
      "Reversed number: " + flipInt( +prompt("Enter a value") ) 
    )

    The above code uses bitwise operators for quick math

    This method is MUCH FASTER than other methods which convert the number to an Array and then reverse it and join it again. This is a low-level blazing-fast solution.

    Illustration table:

    const delay = (ms = 1000) => new Promise(res => setTimeout(res, ms))
    const table = document.querySelector('tbody')
    
    async function printLine(s1, s2, op){
      table.innerHTML += `<tr>
        <td>${s1}</td>
        <td>${s2||''}</td>
      </tr>`
    }
    
    async function steps(){
      printLine(123)
      await delay()
      
      printLine('12.3 →')
      await delay()
      
      printLine(12, 3)
      await delay()
      
      printLine('1.2', '3 &times; 10')
      await delay()
      
      printLine('1.2 →', 30)
      await delay()
      
      printLine(1, 32)
      await delay()
      
      printLine(1, '32 &times; 10')
      await delay()
      
      printLine('1 →', 320)
      await delay()
      
      printLine('', 321)
      await delay()
    }
    
    steps()
    table{ width: 200px; }
    td {
      border: 1px dotted #999;
    }
    <table>
      <thead>
        <tr>
          <th>Current</th>
          <th>Output</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-11-29 12:15

    Using JavaScript reverse() and Math.sign() you can reverse a number both positive and negative numbers.

    var enteredNum = prompt("Enter integer");
    
    function reverseInteger(enteredNum) {
        const reveredNumber = enteredNum.toString().split('').reverse().join('');
        return parseInt(reveredNumber)*Math.sign(enteredNum);
    }
    
    alert(reverseInteger(enteredNum));
    
    0 讨论(0)
  • 2020-11-29 12:20

    Apply Logic of reversing number in paper and try,and you have to care about dividing because it gives float values thats why we have to use parseInt().

    <head>
    <script>
        function palindrome()
        {
            var a = document.getElementById('str').value;
            var r=0 ,t=0;
            while(a>0){
                r=a%10;
                t=t*10+r;
                a=parseInt(a/10);
            }
            
            document.write(t);
            
        }
        </script>
    
    </head>
    
    <body>
    <form>
        <input type="text" id="str"/>
        <input type="submit" onClick="palindrome()" />
    <form>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题