Convert HTML Entities - some issues

Tell us what’s happening:
Hello,

Some of the tests don’t work…
convertHTML(“Hamburgers < Pizza < Tacos”), for example : https://jsfiddle.net/9zm5x4g1/
convertHTML(‘Stuff in “quotation marks”’)
convertHTML(“Sixty > twelve”)
convertHTML("<>") should return &​lt;&​gt; .

I dont get why, in JSbin the regex works.

Your code so far


function convertHTML(str) {
  var regexTable =  {
    '/</gi': '&lt;',
    '/>/gi': '&gt;',
    '&': '&amp;',
    '"': '&quot;',
    '\'': '&apos;'
    };

  let result = str;

  var regexKeys = Object.keys(regexTable);
  
  for (var i=0; i<regexKeys.length; i++) {
   
    result = result.replace(regexKeys[i], regexTable[regexKeys[i]]);
  }

  return result;
}

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/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities

Consider the order in your object
If you change first < to &lt; and then your code check for any & to replace, what happen?

Do you really need the gi flags for only two of your characters?

I added gi flags everywhere…
I m not sure to understand below :

If you change first < to &lt; and then your code check for any & to replace, what happen?

I changed :

var regexTable =  {
    '/&lt;/gi': '&lt;',
    '/>/gi': '&gt;',
    '/&/gi': '&amp;',
    '/"/gi': '&quot;',
    '/\'/gi': '&apos;'
    };

it fails :o i dont get your point actually ;/

Let’s take this.
At the beginning is

"Sixty > twelve"

Then your code iterate over each property on the element, the first one is '<', there is not one in your string so is not replaced, then '>', and your code becomes

"Sixty &gt; twelve"

So now there is '&', and your code becomes

"Sixty &amp;gt; twelve"

Got it now?

Anyway, replace doesn’t need a g tag, and the i just means that there is no distinction between lowercase and uppercase but you are not dealing with letters, so it is not actually needed
Also because you would need to convert that string to a regex before being able to use it. Right now it is just replacing all those characters literally, which means that nothing is being replaced because you don’t have "/>/gi" inside your string


(The last version of your code will never convert < to the proper html element, even considering the above, why did you changed it to be "&lt;"?)

1 Like

Solved below ! I had to use g in var regex, because when i had to detect twice > or < I needed to find and replace all matches.

function convertHTML(str) {
  var regexTable =  {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    '\'': '&apos;'
    };

  let result = str;

  var regexKeys = Object.keys(regexTable);
  
  for (var i=0; i<regexKeys.length; i++) {
   
    let regex = new RegExp(regexKeys[i], 'g');
    result = result.replace(regex, regexTable[regexKeys[i]]);
  }

  return result;
}

Yeah, you are right there for the g flag