Reverse integer

    0

    0

    pavan koka

    The code snippet will take a string x and convert it to a number. It will then look to see if x is less than zero and if it is, it will set isNegative to true and convert the number to a reverse string. The reverse string will then be divided by 2, multiplied by 31 and rounded down to return the original number.

    var reverse = function(x) {
      let number = x.toString()
      let isNegative = false
      if(x < 0) {
        isNegative = true
        number = number.substring(1)
      }
      const reverse = number.split('').reverse().join('');
      if(reverse > (Math.pow(2,31) - 1)){
        return 0;
      } else if (isNegative && reverse > Math.pow(2,31)) {
        return 0
      }
      return isNegative ? -1 * parseInt(reverse) : parseInt(reverse)
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.