Convert HTML entities help needed


function convertHTML(str) {
 


 let char = [{
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&apos;"
  }]

  
  char.filter(function(val){
    for(let keys in val){
      for(let letter in str){
        if(keys == str[letter]){
          str = str.replace(str[letter], val[keys]);
        }
      }
    }
  });
  
  console.log(str);
  // `&colon;&rpar;
  return str;
}

convertHTML('Stuff in "quotation marks"');

Guys need help this code works for all other test for the challenge expect this one mentioned here.

the output that i get is Stuff in &quot;quotation marks

while the expected output is Stuff in &quot;quotation marks&quot;

the challenge is https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities

Don’t use a for-in loop you intend to modify the thing you’re iterating over. In this case, str is reassigned inside the for (let letter in str) loop. Use a standard loop instead.

Also putting the entities object in an array is not necessary. Just assign that object to char directly. By extension, the filter call should also be dropped (and it’s not really a job for filter).

1 Like

@kevcomedia Thanks for the quick help.

str.replace() will iterate over each character in the string, so putting it inside another loop makes a very little sense. Try to solve it in following way:

function convertHTML(str) {
  const charDict = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&apos;"
  };

  return str.replace( /* Visit link below and finish this function */ );
}
1 Like