code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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