Convert HTML Entities challenge

Convert HTML Entities challenge
0

#1

Hello, I tried to come up with a simple solution to the challenge by using a switch case, which turns out to be not so simple.

Here’s what I have at the moment:

function convertHTML(str) {
  // :)
  var words = str.split("");
  return words.map( word => {
  switch(word) {
      case "&":
      return "&​amp;";
      break;
      case "<":
      return "&​lt;";
      break;
      case ">":
      return "&​gt;";
      break;
      case "<":
      return "&​lt;";
      break;
      case "\"":
      return "&​quot;";
      break;
      case "'":
      return "&​apos;";
      break;
      default:
      return word;
      }
  }).join("");
}

convertHTML("Dolce & Gabbana");
console.log(convertHTML("Dolce & Gabbana"));
console.log(convertHTML("Hamburgers < Pizza < Tacos"));</code>

The output are:

Dolce &​amp; Gabbana
Hamburgers &​lt; Pizza &​lt; Tacos

which seems to be correct to me? So I’m stuck wondering where went wrong…any ideas why? Thanks!


#2

The case where you say case """ shouldn’t be correct. You should scape the quote: case "\"".

And are you using this quotes “ or this ones "?


#3

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 easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

If you copied and pasted the replacement values from somewhere, it is possible that you picked up some extra hidden characters when you pasted them into your code. Try deleting the replacement values and manually typing them just in case you had some hidden characters in there. The FCC tests are not expecting to see any hidden characters, so they could be failing because of them.


#4

Formatting the code made my comment completely useless. Lol


#5

Yeah, that can happen. Unformatted code make quotes show up as curly quotes.


#6

Yes, that solved it. The hidden characters made the tests not passing, I simply copy and paste back which solved the issue. Thanks alot!