less than 1 minute read

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” Example 2:

Input: address = “255.100.50.0” Output: “255[.]100[.]50[.]0”

/**
 * @param {string} address
 * @return {string}
 */
var defangIPaddr = function (address) {
  return address.split(".").join("[.]");
};

var defangIPaddr = function(address) { ... }: This line declares a variable defangIPaddr and assigns it an anonymous function. The function takes a single parameter address, which is expected to be a string.

return address.split(".").join("[.]"): Inside the function, the input address is processed. The split(".") method is used to split the string at every occurrence of a period (dot), resulting in an array of substrings.

.join("[.]"): The join() method is then used on the array of substrings to concatenate them back into a string, where each element of the array is separated by the string “[.]” instead of the original periods.

Leave a comment