Intermediate Algorithm Scripting - Convert HTML Entities

hello,
when i test my code in browser ,i get the correct result but freecodecamp console don’t validate my code
any help please
thank you

function convertHTML(str) {
const entities={
  '&':"&",
  '<':"&lt;",
  '>':"&gt;",
  '"':'&quot;',
  "'":'&apos;'
}
for(key in entities){
  regex=new RegExp(key,'g')
  while(match=regex.exec(str)){
  str=str.replace(key,entities[key])
  }
}
return str;
}
  **Your browser information:**

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

Challenge: Intermediate Algorithm Scripting - Convert HTML Entities

Link to the challenge:

try to use proper keyword to declare this

hi , thank you for your reply, i added the keyword let but still can’t validate my code

function convertHTML(str) {
  const entities={
    '&':"&amp;",
    '<':"&lt;",
    '>':"&gt;",
    '"':'&quot;',
    "'":'&apos;'
  }
  for(key in entities){
    let regex=new RegExp(key,'g')
    while(match=regex.exec(str)){
    str=str.replace(key,entities[key])
    }
  }
  return str;
}

log out str before returning like below. That will help to debug it.

function convertHTML(str) {
  const entities={
    '&':"&amp;",
    '<':"&lt;",
    '>':"&gt;",
    '"':'&quot;',
    "'":'&apos;'
  }
  for(let key in entities){
    console.log(key)
    let regex=new RegExp(key,'g')
    while(match=regex.exec(str)){
    str=str.replace(key,entities[key])
    }
  }
  console.log(str)
  return str;
}

and also:
in FCC console, add function call for your code

function convertHTML(str) {
  const entities={
    '&':"&amp;",
    '<':"&lt;",
    '>':"&gt;",
    '"':'&quot;',
    "'":'&apos;'
  }
  for(key in entities){    
    let regex=new RegExp(key,'g')
    while(match=regex.exec(str)){
    str=str.replace(key,entities[key])
    }
  }
  console.log('str before returning', str)
  return str;
}

convertHTML("Dolce & Gabbana")

You will see an error in the console.

thank you for your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.