Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 28, 2018 04:02
Show Gist options
  • Select an option

  • Save lienista/120dc55de25c574063fc2b323acf58cf to your computer and use it in GitHub Desktop.

Select an option

Save lienista/120dc55de25c574063fc2b323acf58cf to your computer and use it in GitHub Desktop.
reverse an unsigned integer
const reverse = (x) => {
if(x === ''){
return '';
} else if(x%10 === 0) {
x = parseInt(x/10);
return reverse(x);
} else if(x < 0) {
x = -x;
return reverse(x)*(-1);
} else {
x = x + '';
let newX = reverse(x.substr(1)) + x.charAt(0);
return parseInt(newX);
}
}
let x = -123456;
let y = reverse(x);
console.log(y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment