2018-10-12 15:37:13 -04:00
---
title: Convert HTML Entities
---
2019-05-26 13:23:41 +01:00
# Convert HTML Entities
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
* 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.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
* You can use regular Expressions however I didn't in this case.
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
* You will benefit from a chart with all the html entities so you know which ones are the right ones to put.
2019-07-24 00:59:27 -07:00
### Hint 3
2018-10-12 15:37:13 -04:00
* You should separate the string and work with each character to convert the right ones and then join everything back up.
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```javascript
2019-05-26 13:23:41 +01:00
function convertHTML(str) {
// Split by character to avoid problems.
2019-07-24 00:59:27 -07:00
var temp = str.split("");
2019-05-26 13:23:41 +01:00
// Since we are only checking for a few HTML elements, use a switch
for (var i = 0; i < temp.length ; i + + ) {
switch (temp[i]) {
2019-07-24 00:59:27 -07:00
case "< ":
temp[i] = "< ";
2019-05-26 13:23:41 +01:00
break;
2019-07-24 00:59:27 -07:00
case "& ":
temp[i] = "& ";
2019-05-26 13:23:41 +01:00
break;
2019-07-24 00:59:27 -07:00
case ">":
temp[i] = "> ";
2019-05-26 13:23:41 +01:00
break;
case '"':
2019-07-24 00:59:27 -07:00
temp[i] = "" ";
2019-05-26 13:23:41 +01:00
break;
case "'":
temp[i] = "' ";
break;
2018-10-12 15:37:13 -04:00
}
2019-05-26 13:23:41 +01:00
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
temp = temp.join("");
2019-05-26 13:23:41 +01:00
return temp;
}
//test here
convertHTML("Dolce & Gabbana");
2018-10-12 15:37:13 -04:00
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
2019-02-11 05:17:34 -06:00
* 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 `'&'` in line 51)
* `temp.join('')` converts the array of characters into a string to be returned.
2018-10-12 15:37:13 -04:00
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split' target = '_blank' rel = 'nofollow' > str.split()</ a >
* < 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 >
2019-07-24 00:59:27 -07:00
< / details >
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-05-26 13:23:41 +01:00
```javascript
function convertHTML(str) {
// Use Object Lookup to declare as many HTML entities as needed.
const htmlEntities = {
2019-07-24 00:59:27 -07:00
"& ": "& ",
"< ": "< ",
">": "> ",
'"': "" ",
"'": "' "
};
2019-05-26 13:23:41 +01:00
// Using a regex, replace characters with it's corresponding html entity
return str.replace(/([&< >\"'])/g, match => htmlEntities[match]);
}
// test here
convertHTML("Dolce & Gabbana");
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2019-05-26 13:23:41 +01:00
* 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` .
2018-10-12 15:37:13 -04:00
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace' target = '_blank' rel = 'nofollow' > str.replace()</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp' target = '_blank' rel = 'nofollow' > Regular Expressions</ a >
2019-07-24 00:59:27 -07:00
< / details >
< details > < summary > Solution 3 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-05-26 13:23:41 +01:00
```javascript
function convertHTML(str) {
// Use Object Lookup to declare as many HTML entities as needed.
2019-07-24 00:59:27 -07:00
const htmlEntities = {
"& ": "& ",
"< ": "< ",
">": "> ",
'"': "" ",
"'": "' "
2019-05-26 13:23:41 +01:00
};
//Use map function to return a filtered str with all entities changed automatically.
2019-07-24 00:59:27 -07:00
return str
.split("")
.map(entity => htmlEntities[entity] || entity)
.join("");
2019-05-26 13:23:41 +01:00
}
// test here
convertHTML("Dolce & Gabbana");
2018-10-12 15:37:13 -04:00
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
2019-05-26 13:23:41 +01:00
* 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.
2018-10-12 15:37:13 -04:00
* 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
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split' target = '_blank' rel = 'nofollow' > str.split()</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map' target = '_blank' rel = 'nofollow' > arr.map()</ a >
* < a href = 'https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join' target = '_blank' rel = 'nofollow' > arr.join()</ a >
2019-07-24 00:59:27 -07:00
< / details >