8000 [ADD] html entities added · luisprooc/js-algorithms@6d623de · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d623de

Browse files
committed
[ADD] html entities added
1 parent 0210050 commit 6d623de

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,26 @@ sumPrimes(10) should return 17.
627627
628628
sumPrimes(977) should return 73156.
629629
630+
```
631+
632+
633+
## Convert HTML Entities
634+
635+
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
636+
637+
```javascript
638+
convertHTML("Dolce & Gabbana") should return the string Dolce &amp; Gabbana.
639+
640+
convertHTML("Hamburgers < Pizza < Tacos") should return the string Hamburgers &lt; Pizza &lt; Tacos.
641+
642+
convertHTML("Sixty > twelve") should return the string Sixty &gt; twelve.
643+
644+
convertHTML('Stuff in "quotation marks"') should return the string Stuff in &quot;quotation marks&quot;.
645+
646+
convertHTML("Schindler's List") should return the string Schindler&apos;s List.
647+
648+
convertHTML("<>") should return the string &lt;&gt;.
649+
650+
convertHTML("abc") should return the string abc.
651+
630652
```

src/convertHtml.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function convertHTML(str) {
2+
3+
switch(true){
4+
case /&/.test(str):
5+
str = str.replace(/&/g,"&amp;");
6+
7+
case /</.test(str):
8+
str = str.replace(/& 9A7C lt;/g,"&lt;");
9+
10+
case />/.test(str):
11+
str = str.replace(/>/g,"&gt;");
12+
13+
case /"/.test(str):
14+
str = str.replace(/"/g,"&quot;");
15+
16+
case /'/.test(str):
17+
str = str.replace(/'/g,"&apos;");
18+
19+
default:
20+
return str;
21+
};
22+
23+
}
24+
25+
console.log(convertHTML('Stuff in "quotation marks"'));

0 commit comments

Comments
 (0)
0