### leetcode: reverse integer: correct answer marked wrong

Posted on Updated on

I was doing leetcode’s reverse integer which as it’s name implies expects an integer to be reverse. example: 562 should be returned as 265.

The website marked my solution wrong even though my solution returned a revered number. The testcase 1534236469 had returned 9646324351 by my solution. Leetcode expected 0

The reason this is happening is because leetcode expects to only deal with a “32-bit signed integer”, and the output is larger than the maximum value of a 32-bit signed integer.

The largest a 32 bit signed integer can be is 2,147,483,647. A bit smaller than 9,646,324,351. I adjusted my solution and now received an acceptance.

``````var reverse = function(x) {
let isNegative = x < 0 || false
let rArray = x.toString().split("")
console.log("rArray is",rArray)
if(isNegative){ rArray.shift()}
let rInt = parseInt(rArray.reverse().join(""))
if (rInt > 2147483647) {return 0}
return (isNegative ? rInt - 2 * rInt : rInt)
};
//pseudocode
//convert to string //split into array
// if  negative is true shift first char
//reverse array // convert to string //convert to num
//if negative true, subtract 2 * x``````