Need help with this Algorithm problem

Tell us what’s happening:

So it seems that it is not converting the last item and that’s weird. Not sure why.

Your code so far


function convertHTML(str) {
var tempArr = str.split('');
console.log(tempArr);
tempArr.forEach((item,index)=>{
  switch(item){
    case '&':
    tempArr.splice(index,1,'&','a','m','p',';');
    break;
    case '<':
    tempArr.splice(index,1,'&','l','t',';');
    break;
    case '>':
    tempArr.splice(index,1,'&','g','t',';');
    break;
    case '\"':
    tempArr.splice(index,1,'&','q','u','o','t',';');
    break;
    case '\'':
    tempArr.splice(index,1,'&','a','p','o','s',';');
    default:
    break;
  }
})
tempArr = tempArr.join('');
console.log(tempArr);
return tempArr;
}

convertHTML("Dolce & Gabbana");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Convert HTML Entities

Link to the challenge:

it can happen when you are iterating over an array and you change it - check what you see if you add console.log(item, index) in the function

1 Like

true. i actually made a mistake of inserting items separately instead of one whole string. Thanks!