Hello! I’m trying to solve this challenge in “Intermediate Algorithms” and I’m running into a problem. The problem is asking to replace HTML entity symbols with their correct representation (i.e. & becomes &). Essentially, when I’m doing a switch-case to find the correct substitute, the console output converts back to the exact symbol I’m trying to convert. Code is below - it’s incomplete, as I’m simply trying to debug.
function convertHTML(str) {
// :)
var regex = /&|<|>|'|"/
var replacer = ""
var checker = str.match(regex).toString()
console.log(regex.test(str))
if (regex.test(str))
{
switch (checker)
{
case "&":
replacer = '&'
break;
case "<":
replacer = "<"
break;
case ">":
replacer = ">"
break;
case "'":
replacer = "'"
break;
case '"':
replacer = """
break;
}
}
console.log(replacer)
return str;
}
convertHTML("Dolce & Gabbana");
As I said in the introduction, console.log(replacer) gives out “&” instead of “& amp;”. In fact, I just found out it happens in this very text box, so I had to insert a space in there. Is there any way I can do the correct substitution? An explanation as to why this is happening would be priceless; I find string operations extremely confusing. Thank you!