Convert HTML Entities, should pass

Convert HTML Entities, should pass
0

#1

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 (str.search(regex5)) {
        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++) {
        replaceEntities(str[i])
      }
      return newStr;
    }

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

Your browser information:

User Agent is: Chrome.

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


#2

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.


#3

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.


#4

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
this:

'&amp;'

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

'&​amp;'

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!


#5

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.


#6

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;'; 

#7

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.


#8

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


#9

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