Intermediate Algorithm Scripting: Convert HTML Entities Bug?

Hi all. I’m new to programming but enjoying the algorithm challenges so far in javascript. I’m not sure but I think I encountered a bug since my solution works on both Repl.it and Brackets but doesn’t seem to work here. So please let me know what I am doing wrong if this is not a bug. Thank you!

function convertHTML(str) {

// :)

return str = str.replace(/&/g, '&​amp;').replace(/</g, '&​lt;').replace(/>/g, '&​gt;').replace(/"/g, '&​quot;').replace(/'/g, '&​apos;');

}

console.log(convertHTML("Hamburgers < Pizza < Tacos"));
console.log(convertHTML("Dolce & Gabbana"));
console.log(convertHTML("Sixty > twelve"));
console.log(convertHTML('Stuff in "quotation marks"'));
console.log(convertHTML("Schindler's List"));
console.log(convertHTML("<>"));

@CaraLagumen

str.replace(/&amp;/g, '&amp;​amp;')

To me, this means for all instances of &amp;in a string, replace it with &amp;​amp;. Is that what you intended? For finding ampersands in a string, the /&/g regular expression works.

Hopefully that’s enough of a hint for you to solve this.

Oops. Something went wrong with the copy paste. This isn’t what I had. I’m not sure how the amp got there for each one. Wtf. :joy: But yea, my problem should be without the amps. Let me see if I can fix this on mobile. Sorry for this. But my first point should still stand that my solution couldn’t work.

This is what I had.:sweat_smile:

function convertHTML(str) {
  // &colon;&rpar;
  return str = str.replace(/&/g, '&​amp;').replace(/</g, '&​lt;').replace(/>/g, '&​gt;').replace(/"/g, '&​quot;').replace(/'/g, '&​apos;');
}

@CaraLagumen it appears to be something with the copy/paste that’s messing things up. When I copy/paste your code into the challenge editor, all the tests fail. When I delete and re-type your HTML codes (i.e. '&lt;') in the editor to match what you have, the tests pass. I don’t think this is a bug with the challenge, but is something strange happening with the copy/paste instead.

This same thing recently happened to someone else: Convert HTML Entities - Chrome console shows right answers

1 Like

Oh I see… Thank you so much. I like to use replit since I console log to see my work then copy paste them. That explains a lot. I will type everything from now on. Thank you!!!:sweat_smile:

Hey. Ran into a problem with this:

function convertHTML(str) {
  var replaceChars = {
    "&" : "&amp;amp;", 
    "<" : "&amp;lt;", 
    ">":"&amp;gt;", 
    "\"":"&amp;apos;", 
    "\'": "&amp;apos;"
  };
  return str.replace(/&|<|>|"|'/g,function(match) {
    return replaceChars[match];
    })
}

My console.log it’s ok, but I still get an error!
Thanks in advance!

btw, I’m using chrome, and this pass the test!

function convertHTML(str) {
  var replaceChars = {
    "&" : "&amp;", 
    "<" : "&lt;", 
    ">":"&gt;", 
    "\"":"&quot;", 
    "\'": "&apos;"
  };
  return str.replace(/&|<|>|"|'/g,function(match) {
    return replaceChars[match];
    })
}

I had the same issue as well. Also copy and pasting from Repl.it. Had to type it out. Wasted a few hours thinking my code was wrong.

Wanted to weigh in and say I had the same issue. Instead of typing out the HTML, I just copied it from the pass conditions, which wouldn’t pass, but typing it out manually (or copying the exact same solution from the hint page) worked fine. Not sure why this happens, but it definitely caused me to waste a lot of time.

If you copy and paste from the description you can find invisible characters in your code. You don’t see them but the code does so the tests don’t pass