Convert HTML Entities - is it a bug?

Tell us what’s happening:
Hello, guys!

I already pass the challenge with replace, but where is an a mistake in my first version with switch statment i still can’t understand

Can someone to explain to me why the code can’t pass test cases?
I tried to rewrite code a bit few times, but it’s still not pass
However, for my opinion and tests the code make convert correctly

Your code so far


function convertHTML(str) {
  let strArr = str.split('');
  for (let i = 0; i < strArr.length; i++) {
    switch(strArr[i]) {
      case '&':
        strArr[i] = '&​amp;';
        break;
      case '<':
        strArr[i] = '&​lt;';
        break;
      case '>':
        strArr[i] = '&​gt;';
        break;
      case '"':
        strArr[i] = '&​quot;';
        break;
      case "'":
        strArr[i] = '&​apos;';
        break;
    }
  }
  strArr = strArr.join('');
  return strArr;
}

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

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

This bug is a little bizarre. I’m guessing you copy/pasted the entities from the test descriptions, which “escape” the entities in a rather unusual way, and as a result, you get this:

'&​amp;' === '&amp;' //false
'&​amp;'.length //6
'&​amp;'.split(''); //["&", "​", "a", "m", "p", ";"]
'&​amp;'.codePointAt(1).toString(16); //"200b"

That codepoint, U+200B, is a zero-width space. It’s one of a few zero-width characters in Unicode that are very useful for specialized applications but can also poop on your parade when you least expect it.

Delete all those zero-width spaces and your code should pass the test cases with no problems.

Thank for you help!
Now it works

Damn, this happened to me, too… I copied those character entities from test cases, because i didn’t want to type them manually, and apparently they are wrong.
Strange…

1 Like