Convert HTML Entities: Failing last 2 tests


I can’t pass the last 2 tests. I suspect it has something to do with my RegExp() but I really don’t know. It’s skipping some of the characters in the string for some reason on those particular tests.

Link to the challenge
Link to my CodePen


I figured it out. conv was the problem. I got rid of it and replaced it with str. good job team

const asci = {
  "&": "&",
  "<": "&lt;",
  ">": "&gt;",
  '"': "&quot;",
  "'": "&apos;"

function convertHTML(str) {
  let conv = ""; // holds converted string to be returned
  let char = Object.keys(asci); //array of keys from the asci object => { // loops through  keys 
    for (let s of str) { loops through string
      if (s === i) { // if key matches str symbol
        let rx = new RegExp(i, 'g') // symbol for matching
        conv = str.replace(rx, asci[i]); // replaces symbols and ..... ooooooh  

  return conv;

convertHTML("Dolce & Gabbana");

Why ‘abc’ fails
Variable conv is declared as empty string. If no characters match one of the 5 special characters your function does not execute any code that would change that. In case of “abc” conv is declared an empty string, the string is looped over and then conv is returned still empty

if (s === i) { // since this never happens
        let rx = new RegExp(i, 'g') 
        conv = str.replace(rx, asci[i]); // then this never happens either

Why ‘<>’ fails
A new string conv is created everytime a special character is encountered. That string will be the original string with that one special character converted to an entity. This works when there is only one special character in the string. When there is more than one, the returned string is the original string with the last special character converted to an entity.

In the case of “<>”
After first pass conv is “&lt;>”
On second pass the new conv is “<&gt;” and that is returned