Alternative solution for Convert HTML Entities challenge

I came up by my own with an alternative solution for Convert HTML Entities Challenge.
It might be basic but I’m happy that it looks good and that I didn’t find a similar solution it in the comments of the challenge.
Also I think it’s a good excuse to start participating in the forum :grin:
If you think there is something that I could change to do a better practice, please let me know :wink:
Thanks!


function convertHTML(str) {
 return str.split("").map((char) => char 
 === "&" ? "\&"
 : char === "<" ? "\&lt;"
 : char === ">" ? "\&gt;"
 : char === "\\"" ? "\&quot;"
 : char === "\\'" ? "\&apos;"
 : char).join("")
   
}

console.log(convertHTML("Hamburgers < Pizza < Tacos"));

I am a bit confused as to how what you have written works.
map((char) => char === “&” ? “&” this line will simply return what char already is with no conversion. The whole function as far as I can tell simply returns an unaltered string. Did you perhaps leave out some of your code?

That aside I personally do not like a bunch of chained ternaries as I find them difficult to read, and in this case you have essentially written a switch statement so you might as well use it unless you do not like switch statements, and I personally do not, but they have there place.

1 Like

I assume this is what you had written as a whole? It does look a bit different , but that was just how I’ve written it.

const convertHTML = str => (
   str.split("").map((char) => char === '&' ?
  '&amp;'
  : char === '<' ? '&lt;'
  : char === '>' ? '&gt;'
  : char === '"' ? '&quot;'
  : char === "'" ? "&apos;"
  : char).join("")
)
  



console.log(convertHTML('Hamburgers < Pizza < Tacos'));
1 Like

Nice. but it’s better to use something like replace function for replacing purposes
In code it would be like:

function convertHTML(str) {
      return str.replace('<', '&lt;')
      .replace('>', '&gt;')
       // and so on
}

Also use replaceAll or regular expressions if you had mulitple ‘<’ in your text and you wanted to replace all of them. Good luck :wink:

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

I edited it so you can read it as I wrote it.
Thank you for your advice!

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

I like using a map, though a nested ternary is hard to read. Personally, I prefer a map with a lookup table.

I would not chain a bunch of regex replaces. That would be much slower.

1 Like

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