Pregunta de entrevista de WebMD Health

Standard FizzBuzz, replacing Fizz with Web and Buzz with MD.

Respuestas de entrevistas

Anónimo

8 de oct de 2020

const str = "FizzBuzz"; let newStr = str.replace("Fizz", "Web"); let newNew = newStr.replace("Buzz", "MD"); return newNew;

Anónimo

8 de oct de 2020

let str = "FizzBuzz"; let mapObj = { Fizz:"Web", Buzz:"MD", }; str = str.replace(/Fizz|Buzz/gi, function(matched){ return mapObj[matched]; }); console.log(str)

Anónimo

9 de oct de 2020

Even better: function replaceAll(str,mapObj){ var re = new RegExp(Object.keys(mapObj).join("|"),"gi"); return str.replace(re, function(matched){ return mapObj[matched]; }); } let str = "FizzBuzz"; let mapObj = { Fizz: "Web", Buzz: "MD" }; console.log(replaceAll(str,mapObj))