Yes, type your html entities instead of pasting them
But that is an horrible way to use map(). If you want to do that, use an other method. Documentations below with a suggestion. (Change the callback function, or change the method… if you keep map and use it correctly you can even chain everything together without declaring a new variable)
I think some of the html codes are not what appear on the screen. For example, I delete and re-type just the & character and it passes some of the cases.
I m not understanding about the comment on ‘horrible way to use the map function’? Which part is horrible? The fact that it pushes to another array? I got that from another freecodecamp solution so I thought it is the way to do it.
Map returns an array of what you return in the function. Your code has an array of undefined ripetuted multiple times
The way your code is structured it needs a forEach() which execute a function for each element. The thing is that using map without exploiting the returned array you are making your code use more memory for no reason
Using map, and actually exploiting its characteristics, one solution is:
function convertHTML(str) {
return str.split('').map(
c=>{
switch (c) {
case '&':
return '&';
case '<':
return '<';
case '>':
return '>';
case '"':
return '"';
case "'":
return ''';
default:
return c;
}
}
).join('');
}
console.log(convertHTML("Dolce & Gabbana"));