article

defang an ip address

[WHAT]

  1. [] defang an ip address  -  is the process of taking an ip address value, like 192.168.1.1 and modifying a given element within that value, the dot separator in this case, to wrap it in a pair of square brackets. 
  2. [] Given a valid (IPv4) IP address, return a defanged version of that IP address.  A defanged IP address replaces every period "." with "[.]".
  3. [] solutions are coded in javascript 

[WHY]

  1. [] practice, practice, practice 

[WHERE]

  1. [] the function will take an ip address string and will return a new string with all of the dot/periods in the string replaced with [.] sequence
    1. [] my initial solution was to iterate over the string and replace the target char with the new sequence of chars, 
    2. [] afterthought - WAS a lot easier/simpler/cleaner to just use a regex , dont know what the performance differences would be

[WHEN]

  1. [x] 2019-08-22 (local) 

[EXAMPLE]

// use indexOf to identify each

elements = address.split('.');
elements.forEach( function(element) {element += "[.]");
console.log(elements);

//

var address = '192.168.1.1';
var elements = address.split('.');
elements = elements.forEach(function(element) {element = element + "[.]"});
console.log(elements);

// my solution

var address = '192.168.1.1';
var elements = address.split('.');
console.log(elements);

// elements = elements.forEach((element){return element + "[.]";});

for (i=0; i<elements.length - 1; i++){
    elements[i] = elements[i]+ "[.]";
}
console.log(elements.join(""));

output ==  "192[.]168[.]1[.]1"

// fixed forEach function 

var address = "192.168.1.1";
address = address.split('.');
var address2 = [];
address.forEach(function(element) {
      address2.push(element+'[.]');
});

console.log(address2.join(""));

// still need to remove the last "[.]"

>> 

 

// better solution - regex

 

var defangIPaddr = function(address) { return address.replace(/\./g, "[.]") };

// better solution - using arrary methods

const defangIPaddr = (address) => {
   
return address.split('.').join('[.]')
}

 

[HOW-TO]

  1. [x] the code in the above example section will execute in the editor at the source site listed below, it passes all tests

[REFERENCE]

  1. [] https://leetcode.com/problems/defanging-an-ip-address/
Details Photos Edit more

Details

ID: 5768

NAME: code-quiz-question

DESCRIPTION: code-quiz-question - defang an ip address - defanging is this case is the process of taking an ip address value, like 192.168.1.1 and modifying a given element within that value (the dot aka period separator) in this case, to wrap it ..

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -5

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and