File tree 2 files changed +51
-0
lines changed
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ // --- Directions
2
+ // Write a function that accepts a string. The function should
3
+ // capitalize the first letter of each word in the string then
4
+ // return the capitalized string.
5
+ // --- Examples
6
+ // capitalize('a short sentence') --> 'A Short Sentence'
7
+ // capitalize('a lazy fox') --> 'A Lazy Fox'
8
+ // capitalize('look, it is working!') --> 'Look, It Is Working!'
9
+
10
+ // Solution 1
11
+ // function capitalize(str) {
12
+ // let result = [];
13
+ // for(let word of str.split(" ")) {
14
+ // result.push(`${word[0].toUpperCase()}${word.slice(1)}`)
15
+ // }
16
+ // return result.join(" ")
17
+ // }
18
+
19
+ // Solution 2
20
+ // Create a string with the first element of the string capitalized
21
+ // For each character of the string if the character on the left is space capitalize and add to the result else add it to the result
22
+ function capitalize ( str ) {
23
+ let result = str [ 0 ] . toUpperCase ( ) ;
24
+ for ( let i = 1 ; i <= str . length ; i ++ ) {
25
+ if ( str [ i - 1 ] === ' ' ) {
26
+ result += str [ i ] . toUpperCase ( ) ;
27
+ } else {
28
+ result += str [ i ]
29
+ }
30
+ }
31
+ return result ;
32
+ }
33
+
34
+ module . exports = capitalize ;
Original file line number Diff line number Diff line change
1
+ const capitalize = require ( './index' ) ;
2
+
3
+ test ( 'Capitalize is a function' , ( ) => {
4
+ expect ( typeof capitalize ) . toEqual ( 'function' ) ;
5
+ } ) ;
6
+
7
+ test ( 'capitalizes the first letter of every word in a sentence' , ( ) => {
8
+ expect ( capitalize ( 'hi there, how is it going?' ) ) . toEqual (
9
+ 'Hi There, How Is It Going?'
10
+ ) ;
11
+ } ) ;
12
+
13
+ test ( 'capitalizes the first letter' , ( ) => {
14
+ expect ( capitalize ( 'i love breakfast at bill miller bbq' ) ) . toEqual (
15
+ 'I Love Breakfast At Bill Miller Bbq'
16
+ ) ;
17
+ } ) ;
You can’t perform that action at this time.
0 commit comments