Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Last active April 11, 2022 08:16
Show Gist options
  • Save Andaeiii/d4617148fb911354f4fbbb2a1510fe25 to your computer and use it in GitHub Desktop.
Save Andaeiii/d4617148fb911354f4fbbb2a1510fe25 to your computer and use it in GitHub Desktop.
my LeetCode Reverse Integer solution
/*
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Examples ::
1. Input: x = 123, Output: 321
2. Input: x = -123, Output: -321
3. Input: x = 120, Output: 21
//you could spread a string to an array.. rather than splitting it.
//solution... */
var reverse = function(x) {
let z = (x < 0) ? true : false;
let v = [...Math.abs(x).toString()].reverse().join('');
return Number(z ? ['-', ...v].join('') : v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment