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
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.
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.
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 = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
const options2 = {
regex: /[&<>"']/g,
map: HTMLentities,
};
console.log(replacer("Hamburgers < Pizza < Tacos", options2)); // Hamburgers < Pizza < Tacos