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.
Hint: 2
- You will benefit form a chart with all the html entities so you know which ones are the right ones to put.
Hint: 3
- You should separate the string and work with each character to convert the right ones and then join everything back up.
Spoiler Alert!
Solution ahead!
Basic Solution:
function convert(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] = '<';
break;
case '&':
temp[i] = '&';
break;
case '>':
temp[i] = '>';
break;
case '"':
temp[i] = '"';
break;
case "'":
temp[i] = ''';
break;
}
}
temp = temp.join('');
return temp;
}
Medium Solution
function convert(str) {
//Chaining of replace method with different arguments
str = str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,''');
return str;
}
Advanced Solution
function convert(str) {
// Use Object Lookup to declare as many HTML entities as needed.
htmlEntities={
'&':'&',
'<':'<',
'>':'>',
'\"':'"',
'\'':'''
};
//Use map function to return a filtered str with all entities changed automatically.
return str.split('').map(function(entity){
return htmlEntities[entity] || entity;
}).join('');
}
Code Explanation (Advanced Solution):
- 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.
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
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)