Algorithm convert HTML entities - ashish9342/FreeCodeCamp GitHub Wiki

Algorithm Convert HTML Entities

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program 👥 and write your own code 📝

🏁 Problem Explanation:

  • You have to create a program that will convert HTML entities from string to their corresponding HTML entities. There are only a few so you can use different methods.

💬 Hint: 1

  • You can use regular Expressions however I didn't in this case.

try to solve the problem now

💬 Hint: 2

  • You will benefit from a chart with all the html entities so you know which ones are the right ones to put.

try to solve the problem now

💬 Hint: 3

  • You should separate the string and work with each character to convert the right ones and then join everything back up.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

🔰 Basic Code Solution:

function convertHTML(str) {
  // Split by character to avoid problems.

  var temp = str.split('');

  // Since we are only checking for a few HTML elements I used a switch

  for (var i = 0; i < temp.length; i++) {
    switch (temp[i]) {
      case '<':
        temp[i] = '&lt;';
        break;
      case '&':
        temp[i] = '&amp;';
        break;
      case '>':
        temp[i] = '&gt;';
        break;
      case '"':
        temp[i] = '&quot;';
        break;
      case "'":
        temp[i] = "&apos;";
        break;
    }
  }

  temp = temp.join('');
  return temp;
}

//test here
convertHTML("Dolce & Gabbana");

Code Explanation:

Explain solution here and add any relevant links

Relevant Links

🚀 Run Code

🌻 Intermediate Code Solution:

function convertHTML(str) {
//Chaining of replace method with different arguments
  str = str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,"&apos;");
return str;
}

// test here
convertHTML("Dolce & Gabbana");

🚀 Run Code

Code Explanation:

Explain solution here and add any relevant links

Relevant Links

🚨 Advanced Code Solution:

function convertHTML(str) {
  // Use Object Lookup to declare as many HTML entities as needed.
  htmlEntities={
    '&':'&amp;',
    '<':'&lt;',
    '>':'&gt;',
    '\"':'&quot;',
    '\'':"&apos;"
  };
  //Use map function to return a filtered str with all entities changed automatically.
  return str.split('').map(function(entity){
    return htmlEntities[entity] || entity;
  }).join('');
}

// test here
convertHTML("Dolce & Gabbana");

🚀 Run Code

Code Explanation:

  • Create a object to use the Lookup functionality to easily find the characters.
  • Split the original string by characters and use map to check for the changed html entity or use the same one. Alternatively you could use Regex str.replace(/&|<|>|"|'/gi.
  • The a function is added which is what returns the converted entity or the original one if there is no conversion. If you go the regex route then you just have to return the matched hits. return html[entity];
  • Lastly we join all the characters once again.

Note that if you went the regex route then you don't need to join anything, just make sure you return the whole operation or save it to a variable and then return it.

Relevant Links

🏆 Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat:

Thanks @Rafase282 @jodekirk for your help with Algorithm: Convert HTML Entities

📋 NOTES FOR CONTRIBUTIONS:

  • ⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See 👉 Wiki Challenge Solution Template for reference.

⚠️ **GitHub.com Fallback** ⚠️