-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_sentence_capitalization.js
49 lines (41 loc) · 1.26 KB
/
8_sentence_capitalization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 8. Sentence Capitalization (句子单词大写问题)
* 描述: Write a function that accepts a string. The function should
* capitalize the first letter of each word in the string then
* return the capitalized string.
*
* Examples:
* capitalize('a short sentence') --> 'A Short Sentence'
* capitalize('a lazy fox') --> 'A Lazy Fox'
* capitalize('look, it is working!') --> 'Look, It Is Working!'
*
* 语言: Javascript(ES6)
*/
// method one
function capitalize1(str){
const words = [];
for (let word of str.split(' ')){
words.push(word[0].toUpperCase() + word.slice(1));
}
return words.join(' ');
}
// method two
function capitalize2(str){
let result = str[0].toUpperCase();
for(let i = 1; i < str.length; i++){
if (str[i - 1] === ' '){
result += str[i].toUpperCase();
} else {
result += str[i];
}
}
return result;
}
// check method one
console.log(capitalize1('a short sentence'));
console.log(capitalize1('a lazy fox'));
console.log(capitalize1('look, it is working!'));
// check method two
console.log(capitalize2('a short sentence'));
console.log(capitalize2('a lazy fox'));
console.log(capitalize2('look, it is working!'));