File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /* Returns the number of times a character occurs in a string */
2+ function numCount ( arr , element ) {
3+ // numCount("hello, l") return 2l
4+ var count = 0 ;
5+ for ( var i = 0 ; i < arr . length ; i ++ ) {
6+ if ( arr [ i ] === element ) {
7+ count ++
8+ }
9+ }
10+ return `${ count } ${ element } ` ;
11+ }
12+
13+ /* Uses numCount function to return run length encoding of all characters in a string */
14+ function runLengthEncoding ( str ) {
15+ // runLengthEncoding("Hello World") returns 1h1e3l2o1w1r1d
16+ var stringArray = str . toLowerCase ( ) . replace ( / [ ^ a - z ] / g, "" ) . split ( "" ) ;
17+ var uniqueStringArray = [ ... new Set ( stringArray ) ]
18+ return uniqueStringArray . map ( ( a ) => numCount ( stringArray , a ) ) . join ( "" ) ;
19+ }
20+
21+ /* Reverses words in a sentence */
22+ function reverseSentence ( str ) {
23+ // reverseSentence("Hello World") returns olleH dlroW
24+ return str . split ( " " ) . map ( a => a . split ( "" ) . reverse ( ) . join ( "" ) ) . join ( " " ) ;
25+ }
26+
27+ /* Determines if a string is a pangram or not */
28+ function pangram ( str ) {
29+ // If a set of the string has exactly 26 letters which are all the letters of the English alphabet, it's a pangram
30+ const stringArray = str . toLowerCase ( ) . replace ( / [ ^ a - z ] / g, "" ) . split ( "" ) ;
31+ const uniqueLetters = [ ... new Set ( stringArray ) ] ;
32+ return uniqueLetters . length === 26 ? "Pangram" : "Not pangram"
33+ }
You can’t perform that action at this time.
0 commit comments