Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 73d88ec

Browse files
committed
Sentence Capitalization
1 parent 3a75872 commit 73d88ec

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Once inside, run the command below:
1010
1. String reversal
1111
2. Vowels counter
1212
3. Most recurring character
13+
4. Sentence Capitalization

src/capSentence/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+

src/capSentence/solutions.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+

src/capSentence/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
})

0 commit comments

Comments
 (0)