Intermediate Algorithm Scripting - Convert HTML Entities

Hi! I don´t understand why my code isn´t working? What is strange to me is that, if I console.log() everything, it does work.

Your code so far

function convertHTML(str) {
  if(str.indexOf("&") >= 0){
   str.replace("&","&")
  }
  if(str.indexOf("<") >= 0){
    str.replace("<","&lt;")
  }
  if(str.indexOf(">") >= 0){
    str.replace(">","&gt;")
  }
  if(str.indexOf('"') >= 0){
    str.replace('"',"&quot;")
  }
  console.log(str.indexOf("&"))
  console.log(str.indexOf("&") >= 0)
  console.log(str.replace("&","&amp;"))
  return str
}

console.log(convertHTML("Dolce & Gabbana"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Convert HTML Entities

Link to the challenge:

str.replace() doesn’t work the way you think:

Strings cannot be mutated!

1 Like

Ohh, thank you very much!!!

1 Like