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
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
---
|
||||
title: Convert HTML Entities
|
||||
---
|
||||
|
||||
# Convert HTML Entities
|
||||
|
||||

|
||||
|
||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||
@ -35,12 +38,12 @@ title: Convert HTML Entities
|
||||
|
||||
##  Basic Code Solution:
|
||||
```javascript
|
||||
function convertHTML(str) {
|
||||
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
|
||||
// Since we are only checking for a few HTML elements, use a switch
|
||||
|
||||
for (var i = 0; i < temp.length; i++) {
|
||||
switch (temp[i]) {
|
||||
@ -64,10 +67,10 @@ title: Convert HTML Entities
|
||||
|
||||
temp = temp.join('');
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
//test here
|
||||
convertHTML("Dolce & Gabbana");
|
||||
//test here
|
||||
convertHTML("Dolce & Gabbana");
|
||||
```
|
||||
### Code Explanation:
|
||||
|
||||
@ -82,27 +85,31 @@ title: Convert HTML Entities
|
||||
* <a href='https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join' target='_blank' rel='nofollow'>arr.join()</a>
|
||||
* <a href='https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/switch' target='_blank' rel='nofollow'>switch statement</a>
|
||||
|
||||
 <a href='https://repl.it/CLnP/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
##  Intermediate Code Solution:
|
||||
|
||||
function convertHTML(str) {
|
||||
//Chaining of replace method with different arguments
|
||||
str = str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,"'");
|
||||
return str;
|
||||
```javascript
|
||||
function convertHTML(str) {
|
||||
// Use Object Lookup to declare as many HTML entities as needed.
|
||||
const htmlEntities = {
|
||||
"&" : '&',
|
||||
"<" : "<",
|
||||
">" : ">",
|
||||
'"' : """,
|
||||
"'" :"'"
|
||||
}
|
||||
// Using a regex, replace characters with it's corresponding html entity
|
||||
return str.replace(/([&<>\"'])/g, match => htmlEntities[match]);
|
||||
}
|
||||
|
||||
// test here
|
||||
convertHTML("Dolce & Gabbana");
|
||||
|
||||
 <a href='https://repl.it/CLnQ/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
// test here
|
||||
convertHTML("Dolce & Gabbana");
|
||||
```
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
* **str** is assigned to a new version of **str** that will contain the original string with all HTML entities converted
|
||||
* The **first parameters** in `replace()` contains a regular expression that matches all instances of each HTML entity in **str**
|
||||
* Replace all those instances with the corresponding HTML strings given in the **second parameter** of `replace()`
|
||||
* Finally, the new **str** is returned
|
||||
* 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`.
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
@ -110,8 +117,9 @@ title: Convert HTML Entities
|
||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp' target='_blank' rel='nofollow'>Regular Expressions</a>
|
||||
|
||||
##  Advanced Code Solution:
|
||||
|
||||
```javascript
|
||||
function convertHTML(str) {
|
||||
function convertHTML(str) {
|
||||
// Use Object Lookup to declare as many HTML entities as needed.
|
||||
const htmlEntities={
|
||||
'&':'&',
|
||||
@ -122,18 +130,17 @@ title: Convert HTML Entities
|
||||
};
|
||||
//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");
|
||||
// test here
|
||||
convertHTML("Dolce & Gabbana");
|
||||
```
|
||||
 <a href='https://repl.it/CLnR/0' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### 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];`
|
||||
* 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.
|
||||
|
Reference in New Issue
Block a user