Convert HTML Entities - how do I get to print in my console the raw html?

Tell us what’s happening:
I had a lot of trouble in figuring out what was wrong with this solution (but I solved it)… so I have a simple question: there is a way to have printed somewhere the raw html of a string?

Your code so far


function convertHTML(str) {
  // :)
  return str.replace("&","&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"', '&quot;').replace("'", "&apos;");
}

convertHTML("Dolce & Gabbana");

console.log(convertHTML("Sixty > twelve"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

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

Did that actually pass? When I run the code you posted,the replace(…) expressions are getting hung if there are more than one of a given character (for example, two <, it would catch the first). Instead, I had to use actual regex. For example, /\</g is the regex to find ALL instances of the < char and replace them.

As to your question, simply separate your function into three steps: first, save the results of replace(), perhaps to newStr. Second, console.info(...) or console.log(...) or whatever the value of newStr. Third, return that newStr value.

this one did not pass, I have solved the challege, but I am asking about how to print raw HTML so that next time I realise sooner what the issue is.

Right. So, going by what I suggested (saving the results of replace() to another string), you can easily print either str or newStr to the console, which will print as the string value (raw HTML and all).

1 Like

ok, thank you, I should have been using the browser console :sweat_smile:

1 Like

ah crud. Yeah, I should have been more explicit. Sorry bout that.

1 Like