Convert Html entities - matching criteria failing

Hi, there are a couple of test cases that do not pass with my current code:

  1. The case where there are multiple occurrences of the same entities in a given string
  2. Inner double quotes

Here is my code so far:

function convertHTML(str) {
  const strSplit = str.split('')
  var indexOf = ''
  var newStr = []
  for (let i=0; i<strSplit.length; i++) {
        if (strSplit[i] == '&') {
          indexOf = str.indexOf('&');
          strSplit.splice(indexOf,1,'&amp;');
        }
        if (strSplit[i].match('\'.*?')) {
          indexOf = str.indexOf(strSplit[i].match('\'.*?'));
          strSplit.splice(indexOf,1,'&apos;');
        }
        if (strSplit[i] == '<') {
          indexOf = str.indexOf('<');
          strSplit.splice(indexOf,1,'&lt;');
        }
        if (strSplit[i] == '>') {
          indexOf = str.indexOf('>');
          strSplit.splice(indexOf,1,'&gt;');
        }
        if (strSplit[i].match('\".*?')) {
          indexOf = str.indexOf(strSplit[i].match('\".*?'));
          strSplit.splice(indexOf,1,'&quot;');
        }
        else {
          newStr.push(strSplit[i]);
        }
  }
  return newStr.join('');
}

Test cases that fail:

  1. convertHTML("Hamburgers < Pizza < Tacos") results in Hamburgers &lt; Pizza < Tacos
  2. convertHTML('Stuff in "quotation marks"') outputs Stuff in quotation marks

The indexOf method as you have written it will only return the first index of str each time. However, the indexOf method does accept a second argument which indicates at what index to start searching that you might be able to use and salvage your existing approach here. See indexOf parameters for more information.

Thanks for the tip, I will check up on this.