Skip to content

Instantly share code, notes, and snippets.

@sebastianherman
Created January 10, 2021 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianherman/e609fb45fb70dc3e21a55bf2e93b5800 to your computer and use it in GitHub Desktop.
Save sebastianherman/e609fb45fb70dc3e21a55bf2e93b5800 to your computer and use it in GitHub Desktop.
Intermediate Algorithm Scripting: Convert HTML Entities - freeCodeCamp solution
function convertHTML(str) {
let signs = {'&': '&',
'<': '&lt;',
'>': '&gt;',
'\"': '&quot;',
'\'': '&apos;'};
let signKeys = Object.keys(signs);
console.log(signKeys);
let strArr = str.split(/([&<>"'])/g);
console.log(strArr);
return strArr.map(word => {
if (signKeys.includes(word)) {
return signs[word];
} else {
return word;
}
}).join("");
}
console.log(convertHTML("Dolce & Gabbana"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment