Files
Guayo Mena f5b94b51f1 feat(curriculum): Extend solution explanation for Convert HTML Entities challenge (#35835)
* Extend solution explanation

The explanation for the advanced code solution was a little cumbersome.
It tried to explain the used method, but also tried to explain an alternative solution, both mixed together.
To fix that, I moved everything related to the alternative solution to another paragraph.
Additionally, I added a more detailed explanation about this alternative solution.
Hopefully, with this fixes, both solutions will be clearer to the user.

* Replace intermediate solution

Replace the intermediate solution with a more concise one.

I took the opportunity to eliminate the indenting in all 3 snippets to remove unnecessary blank space on the left.
I also changed the wording on one of the instructions and fixed a typo.

* Remove external links

* Simplity regex declaration

I replaced `new RegExp()` in order to make the solution cleaner and shorter

* Update index.md
2019-05-26 17:53:41 +05:30

7.1 KiB

title
title
Convert HTML Entities

Convert HTML Entities

HTML entities &'<>"

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: 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.

:speech_balloon: Hint: 1

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

try to solve the problem now

:speech_balloon: 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

:speech_balloon: 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!

warning sign

Solution ahead!

:beginner: 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, use 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:

  • Assign temp to str.split(''), which creates an array containing each individual character in the passed in string.
  • Pass each character in the newly created array into a switch() statement.
  • Replace the HTML entities with their corresponding HTML entity string (i.e. '&' becomes '&amp;' in line 51)
  • temp.join('') converts the array of characters into a string to be returned.

:sunflower: Intermediate Code Solution:

function convertHTML(str) {
  // Use Object Lookup to declare as many HTML entities as needed.
  const htmlEntities = {
    "&" : '&amp;',
    "<" : "&lt;",
    ">" : "&gt;",
    '"' : "&quot;",
    "'" :"&apos;"
  }
  // Using a regex, replace characters with it's corresponding html entity
  return str.replace(/([&<>\"'])/g, match => htmlEntities[match]);
}

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

Code Explanation:

  • Create an object to use the Lookup functionality and easily find the characters.
  • Use replace() to replace characters with regex.
  • The first argument for replace() is a regex that catches all the target characters and puts them into a capturing group.
  • The second arguments for replace() is a function with the matched character as a parameter. It returns the correspondant entity from htmlEntities.

:rotating_light: Advanced Code Solution:

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

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

Code Explanation:

  • Create an object to use the Lookup functionality and 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.
  • The a function is added which is what returns the converted entity or the original one if there is no conversion.
  • 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.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: 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. :traffic_light: