Convert HTML Problems

Tell us what’s happening:

Your code so far

function convertHTML(str) {
  // :)
  //var htmlents = ["&", ">", "<",""","'"];
  for(var i = 0; i < str.length; i++){
    if (str.indexOf('\'') >= 0){
      str = str.replace("'", "&apos;");
    }
    else if(str.indexOf('"') >= 0){
      str = str.replace('"', "&quot;");
    }
    else if(str.indexOf("&") >= 0){
     str = str.replace("&", "&amp;");
    }
    else if(str.indexOf('>') >= 0){
      str = str.replace('>', "&gt;");
    }
    else if(str.indexOf('<') >= 0){
      str = str.replace('<', "&lt;");
    }
  }
  return str;
}

convertHTML("Dolce & Gabbana");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/convert-html-entities

Hello everyone, for some reason, whenever I uncomment the & check, I get an infinite loop warning. My code works for every other check. Yes, I know I could have used a case statement.

Thank you very much, this helped me solve the problem!