Implement an HTML Entity Converter - Implement an HTML Entity Converter

Tell us what’s happening:

function convertHTML(str) {
let symbols = {
“&”: “&”,
“<”: “<”,
“>”: “>”,
““”: “””,
‘"’: “'”
}

return str.replace(/[&<>'"]/g, (key) => symbols[key])

}

console.log(convertHTML((‘Stuff in “quotation marks”’)))

Your code so far

function convertHTML(str) {
    let symbols = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        "\"": "&quot;",
        '"': "&apos;"
    }

    return str.replace(/[&<>'"]/g, (key) => symbols[key])
}

console.log(convertHTML(('Stuff in "quotation marks"')))

Your browser information:

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

Challenge Information:

Implement an HTML Entity Converter - Implement an HTML Entity Converter

Ancer

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

2 Likes

Are these special characters matched to their correct HTML entities?

1 Like

In this task, the task is to convert as many html tags as you want. Now in the code, an object has been created. In that object, the symbols have been taken as keys and an array method called replace is used.

In the replace method, 2 arguments are given, the object that has been changed and the object in which it has been changed. In the first argument, I have taken all the symbols from the regular expression, which are inside the brackets, it will search for all of them and that \g means that at the global level and the second argument is the callback function and that function returns the key of the object. You have learned one thing that how the callback function works in arrays and methods.

And once explore this link

Link:- Character reference - Glossary | MDN .

I hope you understand the code, if there is any problem, please ask.

removed by moderator

Congratulations on solving the challenge! You should be proud of your achievement…we are! But we are removing your working solution, so it is not available to others who have not yet done the work to get there. Again, congrats!

1 Like

removed by moderator - refer to above post for the reason

console.log(convertHTML(‘Stuff in “quotation marks”’))
Yes, there was a mistake in my earlier mapping. I corrected it afterward and updated the code with the proper HTML entities. The current code is the corrected version, and that is the one I am referring to