Intermediate Algorithm Scripting - Convert HTML Entities

I passed the test but don’t understand some part of the code. Anybody can explain me how it work?
Part of the code that I don’t understand how it work is here

({
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&apos;'
    }
  [tag])

And the full code is here

function convertHTML(str) {

return str.replace(/[&<>"]|[']/g, 
  tag => ({
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&apos;'
    }
  [tag]));

}

I’m just curious how you came up with this code to pass the test but you don’t understand it?

I got that part here from asafel answered

OK, so “you” didn’t pass the test, asafel did :slightly_smiling_face:

I’m not trying to be a jerk here, but you have to admit that it sounds a little odd when you say you passed the test but then don’t understand the majority of your own code. There is nothing wrong with asking for clarification on code that you found on the Internet and don’t quite understand how it works. But on this forum we are expecting people to come up with their own solutions to these challenges and thus when you say “I passed the test” we expect that you wrote the code, not someone else.

My suggestion would be to start over from scratch and write your own code for passing this challenge. After you have done that, then you can look at other solutions and compare it to yours which is a good way to learn. But you really need to do it yourself first.

And if I have misunderstood something then I apologize. I am just going on what you have given us so far.

Read the docs and look at the replacement function.


The replacement function will be invoked for every match and its return value is used as the replacement text.

tag is each match, and the object is bracketed into using the match as the key which returns the corresponding value.

Here is an example that might be easier to read and understand.

function stringifyNumbers(str) {
  return str.replace(/\d/g, (match) => ({
    0: 'zero',
    1: 'one',
    2: 'two',
  }[match]))
}

console.log(stringifyNumbers('I have 0 peaches, 1 apple and 2 pineapples')) // I have zero peaches, one apple and two pineapples

Edit: Just to be clear.

Asking how code works is perfectly fine and is encouraged.

Passing challenges using code you didn’t write is one thing and you are not alone in doing this. But all this will do is limit your learning which isn’t helpful.

Using code you do not understand in an actual code base is straight-up dangerous as fixing bugs or extending the code when needed will be very hard or even impossible.

1 Like

I already know and that is why I came here to asking how it work .If you don’t mind.
May I ask another question .
I understand [match] is work as a key of object. But I never seen the object syntax like this

{
0: ‘zero’,
1: ‘one’,
2: ‘two’,
}[match])

If I am not wrong{ 0: 'zero', 1: 'one', 2: 'two' }[match]
is same as
const thisObj[match] if const thisObj = { 0: 'zero', 1: 'one', 2: 'two' }
If I am wrong please correcting me.

This:

return ({
0: 'zero',
1: 'one',
2: 'two',
}[match])

can be rewritten as:

const myObj = {
0: 'zero',
1: 'one',
2: 'two',
}
return myObj[match]

The first code is just avoiding the temporary variable.

1 Like

Yes, the object can be static and placed outside the replace function. Which would likely be slightly more performant instead of creating an object literal on each match.

Doing so would also let you create an abstraction.

// This will not work with the challenge as the tests do not know about the options object

function replacer(str, { regex, map }) {
  return str.replace(regex, (match) => map[match]);
}

const numbersToWords = {
  0: "zero",
  1: "one",
  2: "two",
};

const options = {
  regex: /\d/g,
  map: numbersToWords,
};

console.log(replacer("I have 0 peaches, 1 apple and 2 pineapples", options)); // I have zero peaches, one apple and two pineapples

const HTMLentities = {
  "&": "&amp;",
  "<": "&lt;",
  ">": "&gt;",
  '"': "&quot;",
  "'": "&apos;",
};

const options2 = {
  regex: /[&<>"']/g,
  map: HTMLentities,
};

console.log(replacer("Hamburgers < Pizza < Tacos", options2)); // Hamburgers &lt; Pizza &lt; Tacos
1 Like

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