Tell us what’s happening:
My code isn’t passing, in spite of the fact that it seems to be returning the proper results. It’s messy, as I kinda got caught up with my own spaghetti while I was making it, but before I refactor I just wanted to find out what’s going on here.
Your code so far
function convertHTML(str) {
let symbolsArr = ["&", "<", ">", '"', "'"];
let resultsArr = ["&", "<", ">", """, "'"]
let regex = /[&<>"']/;
str = str.split("");
let finalStr = [];
str.forEach(x => {
if (!regex.test(x)) {
finalStr.push(x);
} else {
finalStr.push(resultsArr[symbolsArr.indexOf(x)]);
}
});
finalStr = finalStr.join("");
return finalStr;
}
convertHTML("Dolce & Gabbana");
My understanding is, when utilizing .forEach on an array, the first two parameters in the callback function represent the value itself and the index of that value on the array.
Thanks to @camperextraordinaire who suggested that you might have a bad case of 'control-character-isis" which is when weird, hidden, CTRL characters creep into your strings and cause the FCC test cases to fail, I tried retyping your switch solution and it worked once I replaced all your strings with ‘clean’ ones.
function convertHTML(str) {
str = str.split("");
str.forEach((x, y) => {
switch(x) {
case "&":
str[y] = "&";
break;
case "<":
str[y] = "<";
break;
case ">":
str[y] = ">";
break;
case '"':
str[y] = """;
break;
case "'":
str[y] = "'";
break;
default:
str[y] = str[y];
}
});
str = str.join("");
return str;
}
So you can try your luck by copying and pasting what I put here or just go back and retype every string assigned to str[y] from scratch (delete the old string and then type in a new one , don’t cut and paste from someplace else).
Yeah, that makes sense. I mostly put that there for clarity for myself, but now that I look back it really just is wasting an extra line of code. Thanks for the input!