JavaScript - HTML Entities


Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.


/*
case & : &amp
case < : &lt
case > : &gt
case " : &quot
case ' : &apos
*/

Solution Basic approach


function convertHTML(str) {

    var index = str.split("")

    for (var i = 0; i < index.length; i++) {
        if (index[i] === "&") {
            index[i] = "&amp;"
        }
        if (index[i] === "<") {
            index[i] = "&lt;"
        }
        if (index[i] === ">") {
            index[i] = "&gt;"
        }
        if (index[i] === "'") {
            index[i] = "&apos;"
        }
        if (index[i] === '"') {
            index[i] = "&quot;"
        }
    }
    return index.join("");
}

console.log(convertHTML("Dolce & Gab<bana"));
console.log(convertHTML("Hamburgers < Pizza < Tacos"));


Output:


Dolce & Gab<bana
Hamburgers < Pizza < Tacos