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

1108 Defanging An Ip Address

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

/*
 * Given a valid (IPv4) IP address, return a defanged version of that IP address.
 * A defanged IP address replaces every period "." with "[.]".
 *
 * Example 1:
 * Input: address = "1.1.1.1"
 * Output: "1[.]1[.]1[.]1"
 */

function defangingAnIPAddress(address) {
  let res = ''
  for (let index = 0; index < address.length; index++) {
    const element = address[index]
    if (element === '.') {
      res += '[.]'
    } else {
      res += element
    }
  }
  return res
}

test('defanging An IP Address', () => {
  expect(defangingAnIPAddress('1.1.1.1')).toEqual('1[.]1[.]1[.]1')
})

Created 2020-04-20T12:31:51+00:00 · Edit