DEV Community

Francesito
Francesito

Posted on

Solved "Convert HTML Entities" algorithm // FreeCodeCamp

function convertHTML(str) {
  let arr = str.split(' ');
  let regex = [['&','&amp;'],['<','&lt;'],['>','&gt;'],['"','&quot;'],["'",'&apos;']];
  for(let x=0;x<arr.length;x++){
    for(let i=0;i<arr[x].length;i++){
      for(let y=0;y<regex.length;y++){
      (arr[x][i] == regex[y][0]) ?
        str = str.replace(arr[x][i],regex[y][1]) : false;
    }
    }
  }
  return str;
}
console.log(convertHTML("Sixty > twelve"));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)