Convert HTML Entities, should pass

This convoluted solution seems to work in Atom.
Why it doesn’t pass the challenge?

  function convertHTML(str) {
      let newStr = '';
      let regex = /&/g;
      let regex1 = /</g;
      let regex2 = />/g;
      let regex3 = /"/g;
      let regex4 = /'/g;
      let regex5 = /&|<|>|'|"/g;

      if ( {
        newStr = str;

      let replaceEntities = entitie => {
        if (entitie === '&') {
          newStr = str.replace(regex, '&mp')
        } else if (entitie === '<') {
          newStr = str.replace(regex1, '&lt;')
        } else if (entitie === '>') {
          newStr = str.replace(regex2, '&gt;')
        } else if (entitie === '"') {
          newStr = str.replace(regex3, '&quot;')
        } else if (entitie === "'") {
          newStr = str.replace(regex4, '&apos;')

      for (var i = 0; i < str.length; i++) {
      return newStr;

convertHTML('Stuff in "quotation marks"');

Your browser information:

User Agent is: Chrome.

Link to the challenge:

The problem is your for loop executes the replaceEntities function n - 1 times where n is the length of str. Think about what your code assigns to newStr when i is equal to str.length -1.

1 Like

When the for loop executes replaceEntities function, at str.length -1 position
it assigns to the function the last letter. So the fun checks if the last letter is a match.
But why is that a problem?
I tried to iterate until str.length-1, but still nothing.

I tried with switch but again nothing.
I looked at the solution. And there was exactly the same code.
The FCC code passed the test. Mine didn’t.
So I searched it piece by piece.
For an unknown, still to me, reason


would pass.
But mine (which is the same):


would not. This counts for all the other values too.
I then tried to change these values, to the above code.
Only one does not pass.

convertHTML("<>") should return &​lt;&​gt;.

While the others which include these tags, do pass.
The good thing is that I’ve learned again a lot today!

The reason your latest code fails is similar to why your original version failed. You need to walk through the code for the test case of convertHTML("<>") should return &​lt;&​gt;.

There are only two characters (< and >). What does newStr get assigned on the first pass and then the second pass? Add some console.log statements so you can see what the value of newStr in your else if blocks related to the < and > conversions.

1 Like

I will check everything you say. But first I need to tell you about these weird thing that
happend with that code.
I copied the code to a repository in github, and I got red dots in between the code!
You don’t see them down below. But if you try to backspace the entitie,
from ‘a’ to ‘&’ you need one extra hit to delete it.
Also in github I see them only on editing.
When I save it, it’s not visible.

arr[i] = '&​amp;'; 

My guess is you copied/pasted the replacement values from the test results section which currently have some hidden characters which were causing the tests to fail. Once you deleted those hidden characters (backspacing the entire string and retyped the replacement) you were able to have the correct value.

1 Like

Yes I did copy/paste. Good to know. I won’t copy paste again anything from the test results section. Thanks again for your help. I really appreciated it

Honestly, that was a fluke. I am actually working today to get that bug fixed.

1 Like