-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm11.html
48 lines (37 loc) · 1.09 KB
/
algorithm11.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html>
<body>
<script>
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities
//Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
function convertHTML(str) {
//1. Set up a dictionary
var diction = {
"&": "&",
"<" : "<",
">" : ">",
"\"": """,
"'": "'",
};
//1. Find the special characters & Convert the special characters
// for (var i = 0; i<str.length; i++) {
// if (Object.keys(diction).includes(str[i])) {
// str = str.replace(str[i],diction[str[i]]);
// }
// }
return str.split('').map(c => {
if (diction[c]) {
return diction[c];
}
return c;
}).join('');
}
//tests
console.log(convertHTML("Dolce & Gabbana"));
console.log(convertHTML("Hamburgers < Pizza < Tacos"));
console.log(convertHTML('Stuff in "quotation marks"'));
console.log(convertHTML("<>"));
console.log(convertHTML("abc"));
</script>
</body>
</html>