Bug in Hand-written special chacracters and copied ones

I found something mildly interesting in my code in challenge of converting special character.

So my code is:

function convertHTML(str) {
  // const specialCharacters = {
  //       '&':'&​amp;',
  //       '<':'&​lt;',
  //       '>':'&​gt;',
  //       '"':'&​quot;',
  //       '\'':'&​apos;'}
  const specialCharacters={
        '&':'&amp;',
        '<':'&lt;',
        '>':'&gt;',
        '"':'&quot;',
        '\'':"&apos;"
      };
  for (let specialChar in specialCharacters) {
    let reg = RegExp(specialChar,'g');
    if (reg.test(str)) {
      str = str.replace(reg,specialCharacters[specialChar]);
    }
  }
  return str;
}

as you can see, I include 2 set of specialCharacters in my code. The commented object , I copy the characters"&amp"," '&​gt;​" from the instruction box in the challenge, the second part I myself type them out. They look the same, but the seconds work, not the first (Or at least the TFC just accepted the second one). Is there any hidden difference?

It just makes me really both interested and confused. Can somebody explain this to me, please.
Many thanks.

Part of how these characters get formatted for display in the challenge results in “hidden” characters ( \u200b Zero-width space ), between “&” and the rest of the entity. When you copy-paste, you are also copying these characters. I strongly recommend that you always type rather that copy-paste. This isn’t because of formatting issues (which are rare) but because copy-pasting doesn’t help you learn or practice anything.

1 Like

I actually don’t find the benefits of learning &apos; or &quot;. I understand what they mean, and copy as to make it faster. Likewise, I copy alphabet array 'abcdefghijklmnopqrstuvwxyz' instead of writing them word by word. I see no point in doing such things.

I think you mean one should not copy codes, right? I agree with that, but the argument is one should write her own code because she would also learn the logic and thinking patterns. But copying these complex, special key is okay, it hurts nothing from those purposes.

/// Thank you for the explanation on Zero-width Space things.

I’ve always copy pasted entity codes (because screw memorising all the codes I commonly want), and that zero-width character has been present for years and years on a few of the many web pages that carry lists of the things. And this character seems to have kinda slowly spread like a virus (I think it was present on the wikipedia page for entity codes for a little while, something like 10 years ago, maybe that started it)

1 Like