How to replace quotation marks in a string, with the use of objects

I am quite close to solving the Convert HTML Entities challenge, but I have some trouble with the replacements of the quotation marks. As you can see, I have inserted the HTML symbols to be inserted in an object.

Problem is, I cannot get the quotation marks to work properly. convertHTML('Stuff in "quotation marks"') should return "Stuff in "quotation marks"" . But in my case it is not replacing anything, and just returning the same exact sentence back (‘Stuff in “quotation marks”’.)

Will I have to abandon this approach, or is there a way to get the quotation marks to be replaced with "

Your code so far


function convertHTML(str) {
let replacements = {
  "&": "&",
  "<": "&lt;",
  ">": "&gt;",
  '""': "&quot;",//THIS PROBLEM ME MUCH
  "'": "&apos;",
  "<>": "&lt;&gt;",
}
return str.replace(/(&|<|>|""|'|<>)/gi, function(noe) {
  return replacements[noe];
});
}

convertHTML("Dolce & Gabbana");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Convert HTML Entities

Link to the challenge:

you are trying to replace "", so two quotation marks, instead you need to replace ", one single quotation mark. In none of the strings you have two consecutive double quotes

1 Like

Thanks a lot @ilenia! That did the trick! Posting my final code below for reference.

function convertHTML(str) {
  let replacements = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",//THIS PROBLEM ME NO MORE THANKS TO ieahleen
    "'": "&apos;",
    "<>": "&lt;&gt;",
  }
  return str.replace(/(&|<|>|"|'|<>)/gi, function(noe) {
    return replacements[noe];
  });
}

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, 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.

Thank you.

1 Like