Convert HTML entities - is there an issue with my code?

Hey fellow Campers,

I am wondering what seems to be the issue with my code, what the console prints seems to match what the tests are asking for, but they fail.

function convertHTML(str) {
  // :)  
  let rep = { "&": "&​amp;", ">": "&​gt;", "<": "&​lt;", "\"": "&​quot;", "\'": "&​apos;"};

let a = str.replace(/[&|>|<|\"|\']/g, element => rep[element])
return a;

convertHTML("Dolce & Gabbana");
convertHTML("Hamburgers < Pizza < Tacos")
convertHTML('Stuff in "quotation marks"')

The console prints:

Dolce &​amp; Gabbana

Hamburgers &​lt; Pizza &​lt; Tacos

Stuff in &​quot;quotation marks&​quot;


Convert HTML entities

You should replace single quotes with &apos;

You have hidden characters in your HTML entities. Type them out again manually.

Edit: or just use this instead (I remove the \u200b (Zero width space) characters)

let rep = { "&": "&amp;", ">": "&gt;", "<": "&lt;", "\"": "&quot;", "\'": "&apos;" };

1 Like

Thank you, that worked perfectly! :smile: