Intermediate Algorithm Scripting: Convert HTML Entities 2

Tell us what’s happening:
I am trying to separate the argument & & check each one for special characters and if they are, pushing them to an array.

Is this a right approach to go for this challenge?

If it is how can I check for a “” (double quote) or ’ apostrophe in the string.

Your code so far


function convertHTML(str) {
let individualItems = str.split(" ");

let specialCharacters=[];
for (let i=0; i<individualItems.length; i++){
  if (individualItems[i]==="&"){
    specialCharacters.push("&amp")
  }
  if (individualItems[i]==="<"){
    specialCharacters.push("&lt")
  }
  if (individualItems[i]==='""'){
    specialCharacters.push("&quot")
  }
  console.log(specialCharacters)
}
return str;
}

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/86.0.4240.111 Safari/537.36.

Challenge: Convert HTML Entities

Link to the challenge:

By using backticks or by wrapping them in the other type of quote:
Checking for double quote: if (item === '"')
Checking for apostrophe: if (item === "'")

Hi,
Thanks for your reply but it didn’t work

Have you just copy/pasted my code? In that case, item should of course be individualItems[i], I was just too lazy to type that out.

If that doesn’t help, can you share your current code?

Haha, no I didn’t copy pasted your code, I replaced item, but it still isn’t working. Here is the code

function convertHTML(str) {

let individualItems = str.split(" ");

let specialCharacters=[];

for (let i=0; i<individualItems.length; i++){

  if (individualItems[i]==="&"){

    specialCharacters.push("&amp")

  }

  if (individualItems[i]==="<"){

    specialCharacters.push("&lt")

  }

  if (individualItems[i]==='"'){

    specialCharacters.push("&quot")

  }

  if (individualItems[i]==="'"){

    specialCharacters.push("&apos")

  }

  console.log(specialCharacters)

}

return str;

}

convertHTML("Dolce's & Gabbana <");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Thanks, I’ll keep it in mind next time i share my code :slight_smile:

A couple of things to consider:

Your function is receiving str as argument, and at the end, you’re returning str, but your function doesn’t really do anything with it.

In your for-loop, you’re pushing the special characters into an array, but then do nothing with it either. Instead of storing them in an array, you could do something else with them.

Hi,
Thanks for your reply,

I didn’t finish writing the code then, I asked, whether my approach for the code is right & how can I check for double quotes and apostrophes in the string.

Thankfully I get to know how to check for double quotes and apostrophes and the code worked well.

Thanks again for your time