function convertHTML(str) {
const symbols = {
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'"
}
for (const symbol in symbols) {
if (str.indexOf(symbol) >= 0) {
const newStr = str.replaceAll(symbol, symbols[symbol])
return newStr
}
}
return str;
}
I got all of the tests right except for this one:
convertHTML("<>") should return the string <> .
When I enter the string “<>” in the function, it replaces the first arrow but not the second. So I get "<>" as a result. Can anyone tell me why that is?
function convertHTML(str) {
const symbols = {
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'"
}
let newStr = str
for (const symbol in symbols) {
if (str.indexOf(symbol) >= 0) {
newStr = str.replaceAll(symbol, symbols[symbol])
}
}
return newStr;
}
I moved the return keyword outside of the loop and declared the newStr variable outside of the loop as well. I had to make it equal to str at first so the string would stay the same if no symbols are present. I tried it again but instead I get the second arrow replaced instead of the first (<>).
I just want to keep trying my own solutions before I use one of the hints.