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 <> .
I dont get why, in JSbin the regex works.
Your code so far
function convertHTML(str) {
var regexTable = {
'/</gi': '<',
'/>/gi': '>',
'&': '&',
'"': '"',
'\'': '''
};
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.
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 > twelve"
So now there is '&', and your code becomes
"Sixty &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 "<"?)