File tree 4 files changed +76
-0
lines changed
4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -10,3 +10,4 @@ Once inside, run the command below:
10
10
1 . String reversal
11
11
2 . Vowels counter
12
12
3 . Most recurring character
13
+ 4 . Sentence Capitalization
Original file line number Diff line number Diff line change
1
+ // pick a solution and insert here to run the test.
2
+
3
+ function capSentence ( text ) {
4
+ let wordsArray = text . toLowerCase ( ) . split ( ' ' )
5
+
6
+ let capsArray = wordsArray . map ( word => {
7
+ return word . replace ( word [ 0 ] , word [ 0 ] . toUpperCase ( ) )
8
+ } )
9
+
10
+ return capsArray . join ( ' ' )
11
+ }
12
+
13
+ module . exports = capSentence ;
14
+
Original file line number Diff line number Diff line change
1
+ // USING A FOREACH LOOP
2
+ function capSentence ( text ) {
3
+ let wordsArray = text . toLowerCase ( ) . split ( ' ' )
4
+ let capsArray = [ ]
5
+
6
+ wordsArray . forEach ( word => {
7
+ capsArray . push ( word [ 0 ] . toUpperCase ( ) + word . slice ( 1 ) )
8
+ } ) ;
9
+
10
+ return capsArray . join ( ' ' )
11
+ }
12
+
13
+
14
+ // USING .MAP() AND .SLICE()
15
+
16
+ function capSentence ( text ) {
17
+ let wordsArray = text . toLowerCase ( ) . split ( ' ' )
18
+ let capsArray = wordsArray . map ( word => {
19
+ return word [ 0 ] . toUpperCase ( ) + word . slice ( 1 )
20
+ } )
21
+
22
+ return capsArray . join ( ' ' )
23
+ }
24
+
25
+
26
+ // USING .MAP() AND .REPLACE()
27
+
28
+
29
+ function capSentence ( text ) {
30
+ let wordsArray = text . toLowerCase ( ) . split ( ' ' )
31
+
32
+ let capsArray = wordsArray . map ( word => {
33
+ return word . replace ( word [ 0 ] , word [ 0 ] . toUpperCase ( ) )
34
+ } )
35
+
36
+ return capsArray . join ( ' ' )
37
+ }
38
+
Original file line number Diff line number Diff line change
1
+ const capSentence = require ( './index' )
2
+
3
+ test ( 'capSentence is a function' , ( ) => {
4
+ expect ( typeof capSentence ) . toEqual ( 'function' )
5
+ } )
6
+
7
+ test ( 'capitalizes the first letter of each word in a lowercase sentence' , ( ) => {
8
+ expect ( capSentence ( 'i must confess, this is so much fun.' ) ) . toEqual (
9
+ 'I Must Confess, This Is So Much Fun.'
10
+ )
11
+ } )
12
+
13
+ test ( 'capitalizes the first letter of each word in an uppercase sentence' , ( ) => {
14
+ expect ( capSentence ( 'THIS ONE IS FOR SCOTCH.' ) ) . toEqual (
15
+ 'This One Is For Scotch.'
16
+ )
17
+ } )
18
+
19
+ test ( 'capitalizes the first letter of each word in mixed cased sentences' , ( ) => {
20
+ expect ( capSentence ( 'i woulD lOVe to spEAk at jsconF.' ) ) . toEqual (
21
+ 'I Would Love To Speak At Jsconf.'
22
+ )
23
+ } )
You can’t perform that action at this time.
0 commit comments