Convert HTML Entities(<>)

I guess this time there should not be any problem

function convertHTML(str) {
let answer;
if(str.includes('&')) {
   answer = str.replace(/&+/g, '&amp;')
} else if (str.includes('<')) {
   answer = str.replace(/</g, '&lt;')
} else if (str.includes('>')) {
   answer = str.replace(/>/g, '&gt;')
} else if (str.includes('"')) {
   answer = str.replace(/"+/g, '&quot;')
} else if (str.includes("'")) {
   answer = str.replace(/'+/g, '&apos;')
} else {
   answer = str;
return answer;

  **Your browser information:**

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

Challenge: Convert HTML Entities

Link to the challenge:

Which if-case does your function go into with the input <> ?

It is ending up at

 } else if (str.includes('<')) {
     answer = str.replace(/</g, '&lt;')

even if I did not add break there and I Iiterally don’t know why

only one of the if statements execute (that’s how an if/else if/else chain work, the first true condition executes, and nothing else), and then it goes to returning the string

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.