LeetCode problem #7 — Reverse Integer (JavaScript)


For this solution, we take the provided number, get its absolute value (that is to say, we remove any negative symbol), convert it to a string, convert that string to an array, reverse the array, join the array back into a string, parse it into a number, and re-negate it if necessary.

-1234             // Number in
1234 // Absolute value
"1234" // String value
["1","2","3","4"] // Array value
["4","3","2","1"] // Reverse array value
"4321" // Joined array
4321 // Parsed number
-4321 // Negated number



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Duncan McArdle

Duncan McArdle

Full-stack software developer from the UK, author of the Aftermath book series, full time tech-nerd.