xxxxxxxxxx
function convertHTML(str) {
// Use Object Lookup to declare as many HTML entities as needed.
const htmlEntities = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
};
// Using a regex, replace characters with it's corresponding html entity
return str.replace(/([&<>\"'])/g, match => htmlEntities[match]);
}
// test here
convertHTML("Dolce & Gabbana");
xxxxxxxxxx
function convertHTML(str) {
// Use Object Lookup to declare as many HTML entities as needed.
const htmlEntities = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
};
//Use map function to return a filtered str with all entities changed automatically.
return str
.split("")
.map(entity => htmlEntities[entity] || entity)
.join("");
}
// test here
convertHTML("Dolce & Gabbana");