Intermediate Algorithm Scripting: Convert HTML Entities - solution using switch shows expected results but did not pass tests

Intermediate Algorithm Scripting: Convert HTML Entities - solution using switch shows expected results but did not pass tests
0

hey all,

I’m wondering why this code doesn’t pass the tests for https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities
i’ve not read the solution yet, the console.log(str) line shows the output as intended, so I’m kinda curious.

function convertHTML(str) {
    // :)
    for (let i = 0; i < str.length; i++) {
        switch (str[i]) {
            case '&':
                str = str.replace(str[i], '&​amp;');
                break;
            case '<':
                str = str.replace(str[i], '&​lt;');
                break;
            case '>':
                str = str.replace(str[i], '&​gt;');
                break;
            case '\'':
                str = str.replace(str[i], '&​apos;');
                break;
            case '\"':
                str = str.replace(str[i], '&​quot;');
                break;
        }
    }
    // console.log(str);
    return str;
}

convertHTML("Dolce & Gabbana");

You know that you don’t need to loop over the string? Replace on its own will look at all the string and replace if needed

It is highly unadviced to change the length of the string while you are looping over it, it creates unexpected behaviour

I suggest you look at what your code does using the browser console, the fcc console is not really the best that can be

Or on repl.it, or using pythontutor.com for a step by step look at what your code is doing

1 Like

thanks i’ll refactor this :slight_smile:

1 Like