Convert HTML Entities wont work despite of "working"

Tell us what’s happening:
The code below actually returns the desired result (tested in visual studio code)
But the following goals wont pass (despite of them being returned correctly)

convertHTML("Dolce & Gabbana") should return Dolce &​amp; Gabbana
convertHTML("Hamburgers < Pizza < Tacos") should return Hamburgers &​lt; Pizza &​lt; Tacos
convertHTML("<>")` should return  `&​lt;&​gt;

The rest of them actually pass correctly.
Im not sure this is the most efficent way to code the task, but it returns what the task asks for

Your code so far


function convertHTML(str) {
 var splitted = str.split("");
    console.log(splitted);
    for(let i = 0; i<splitted.length;i++){
        if(splitted[i] == '&'){
            splitted.splice(i, 1, '&​amp;');
        }else if(splitted[i] == '<'){
            splitted.splice(i, 1, '&​lt;');
        }else if(splitted[i] == '>'){
            splitted.splice(i, 1, '&gt;');
        }else if(splitted[i] == '"'){
            splitted.splice(i, 1, '&quot;');
        }else if(splitted[i] == "'"){
            splitted.splice(i, 1, '&apos;');
        }
    }
    console.log(splitted.join(""));
    return splitted.join("");
}

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

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

Maybe it is your keyboard or possibly you are copy / pasting the entities from a list.

I removed the entities in your for…loop and re-entered them and then your code passed the test.

Comparing your output character by character to output from a known passing solution I got

D D true
o o true
l l true
c c true
e e true
    true
& & true
a ​ false
m a false
p m false
; p false
  ; false
G   false
a G false
b a false
b b true
a b false
n a false
a n false
undefined 'a' false

For some reason your string has a space between & and amp; in &amp; that does not show up when output to console.

Maybe someone else more knowledgeable will chime in with a definitive answer.

1 Like