Last active
June 15, 2018 17:55
-
-
Save eday69/25bd91db022064256ca84d279c5b3bf0 to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Convert HTML Entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert the characters &, <, >, " (double quote), and ' (apostrophe), | |
// in a string to their corresponding HTML entities. | |
function convertHTML(str) { | |
// :) | |
const convertHTML={ "&": "&", | |
"<": "<", | |
">": ">", | |
"\"": """, | |
"'": "'"} | |
return str.split("").map(letter => { return [ | |
letter.match(/[&<>"']/) ? convertHTML[letter] : letter] | |
}).join(""); | |
} | |
convertHTML("Dolce & Gabbana"); // Dolce & Gabbana | |
convertHTML("Hamburgers < Pizza < Tacos"); // Hamburgers < Pizza < Tacos | |
convertHTML("Sixty > twelve"); // Sixty > twelve | |
convertHTML('Stuff in "quotation marks"'); // Stuff in "quotation marks" | |
convertHTML("Schindler's List"); // Schindler's List | |
convertHTML("<>"); // <> | |
convertHTML("abc"); // abc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment