Tell us what’s happening:
hello everybody, im trying to figure out what is the problem there as i use this switch -case in a for Loop and it works for all the tests except this one ‘<>’. It seems it makes the first replacement right the second also right but it can not keep both changes till the end. It is like for every new change it uses again the initial str and not the newStr. Is there any ideas how i can fix this?
Your code so far
function convertHTML(str) {
// :)
var newStr=str;
var spaceRemover = str.split(' ').join('');
for(i=0; i<str.length; i++){
switch (true) {
case str[i].includes('&'):
newStr=spaceRemover.replace(/&/g, ' & ');
break;
case str[i].includes('<'):
newStr=spaceRemover.replace(/</g, ' < ');
break;
case str[i].includes('>'):
newStr=spaceRemover.replace(/>/g, ' > ');
break;
case str[i].includes('"'):
newStr=newStr.replace(/"/g, '"');
break;
case str[i].includes("'"):
newStr=newStr.replace(/'/g, ''');
break;
}
}
console.log(newStr);
return newStr;
}
convertHTML("<>");
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.
Link to the challenge:
https://www.freecodecamp.org/challenges/convert-html-entities
