Intermediate Algorithm Scripting: Convert HTML Entities Bug?

Intermediate Algorithm Scripting: Convert HTML Entities Bug?
0

#1

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("<>"));

#2

@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.


#3

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.


#4

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;');
}

#5

@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


#6

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:


#7

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];
    })
}