code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

007 Reverse Integer

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function reverseInteger(value) {
  const valueStr = value.toString().split('')
  let joined = parseInt(valueStr.reverse().join(''))
  if (valueStr[valueStr.length - 1] === '-') {
    joined *= -1
  }
  if (joined < Math.pow(2, 31) * -1 || joined > Math.pow(2, 31) - 1) return 0
  return joined
}

test('reverse Integer', () => {
  expect(reverseInteger(-123)).toEqual(-321)
})

Created 2020-04-08T11:21:45+00:00 · Edit