Convert HTML Entities - Not passing tests?

Tell us what’s happening:
My code isn’t passing, in spite of the fact that it seems to be returning the proper results. It’s messy, as I kinda got caught up with my own spaghetti while I was making it, but before I refactor I just wanted to find out what’s going on here.

Your code so far


function convertHTML(str) {
  let symbolsArr = ["&", "<", ">", '"', "'"];
  let resultsArr = ["&​amp;", "&​lt;", "&​gt;", "&​quot;", "&​apos;"]
  let regex = /[&<>"']/;
  str = str.split("");
  let finalStr = [];
  str.forEach(x => {
    if (!regex.test(x)) {
      finalStr.push(x);
    } else {
      finalStr.push(resultsArr[symbolsArr.indexOf(x)]);
    }
  });
  finalStr = finalStr.join("");
  return finalStr;
}

convertHTML("Dolce & Gabbana");

Your browser information:

User Agent is: Chrome/67.0.3396.99.

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

Went ahead and made it a bit simpler with a Switch statement. Seem to be encountering the same issue:

function convertHTML(str) {
  str = str.split("");
  str.forEach((x, y) => {
    switch(x) {
      case "&":
        str[y] = "&​amp;";
        break;
      case "<":
        str[y] = "&​lt;";
        break;
      case ">":
        str[y] = "&​gt;";
        break;
      case '"':
        str[y] = "&​quot;";
        break;
      case "'":
        str[y] = "&​apos;";
        break;
      default:
        str[y] = str[y];
    }
  });
  str = str.join("");
  return str;
}

convertHTML("Dolce & Gabbana");

so can you explain what ‘x’ and ‘y’ are?
if str is [‘D’,‘o’,‘l’,‘c’,‘e’] for eg… what is x and what is y?

My understanding is, when utilizing .forEach on an array, the first two parameters in the callback function represent the value itself and the index of that value on the array.

So if I have…

let arr = [1, 2, 3, 4, 5];
arr.forEach((x, y) => {
});

Then the first time it runs, x will equal “1”, and y will equal “0”.

For the second iteration, x will equal “2”, and y will equal “1”.

And so on and so forth.

If I’ve got my facts wrong, please feel free to let me know. Want to make sure I have everything straight! :slight_smile:

1 Like

Thanks. I just didn’t know…

1 Like

can you try using something other than ‘str’ to compile your final string?

for eg. make a new array like this:
strArr = str.split("");

and then return

return strArr.join(’’);

hi again!

Thanks to @camperextraordinaire who suggested that you might have a bad case of 'control-character-isis" which is when weird, hidden, CTRL characters creep into your strings and cause the FCC test cases to fail, I tried retyping your switch solution and it worked once I replaced all your strings with ‘clean’ ones.

function convertHTML(str) {
str = str.split("");
str.forEach((x, y) => {
switch(x) {
case "&":
str[y] = "&amp;";
break;
case "<":
str[y] = "&lt;";
break;
case ">":
str[y] = "&gt;";
break;
case '"':
str[y] = "&quot;";
break;
case "'":
str[y] = "&apos;";
break;
default:
str[y] = str[y];
}
});
str = str.join("");
return str;
}

So you can try your luck by copying and pasting what I put here or just go back and retype every string assigned to str[y] from scratch (delete the old string and then type in a new one , don’t cut and paste from someplace else).

Your code works perfectly after that…

Oh wow, I’ve never heard of that before! That’s a really good thing to know.

Thanks so much for your help, and thank you to @camperextraordinaire as well! I’m gonna look more into that.

just in case… the ‘control-character-isis’ thing is just me being cheeky…

Oh yeah, I’ve got you. Thanks for clarifying though :slight_smile:

1 Like

Yeah, that makes sense. I mostly put that there for clarity for myself, but now that I look back it really just is wasting an extra line of code. Thanks for the input!