Convert HTML Entities - is it a bug?

Convert HTML Entities - is it a bug?
0.0 0

#1

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


#2

This bug is truly bizarre… at a wild guess, I’d say you copied and pasted those character entities from some other source (no worries, copying the entities themselves isn’t cheating), but it seems that source was written by someone who didn’t know the proper way to write the HTML for their page such that the entity names would show up properly. 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.


#3

Thank for you help!
Now it works