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

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