-
Notifications
You must be signed in to change notification settings - Fork 11
/
2-capitalize-word.js
69 lines (57 loc) · 1.49 KB
/
2-capitalize-word.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/** Capitalize a Word
*
* Implement a function that takes a word and
* return the same word with the first letter capitalized
*
* capitalize('hello')
* Output: 'Hello'
*
* capitalize('GREAT')
* Output: 'Great'
*
* capitalize('aWESOME')
* Output: 'Awesome'
*
*/
// ✅ Solution: github.com/samanthaming/web-basics-challenge
function capitalize(word) {
return (
word.charAt(0).toUpperCase() // Uppercase the first letter
+ word.slice(1).toLowerCase() // Lowercase the rest of the letters
);
}
// ============================
// Using Bracket Notation
// ============================
function capitalize2(word) {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
}
// ============================
// Using Substring
// ============================
function capitalize3(word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase();
}
// ============================
// Using 2 steps
// ============================
function capitalize4(word) {
const loweredCase = word.toLowerCase();
return word[0].toUpperCase() + loweredCase.slice(1);
}
// ============================
// Using Rest parameter
// ============================
function capitalize5([first, ...rest]) {
return first.toUpperCase() +
rest.join('').toLowerCase();
}
// ============================
// Using Map
// ============================
function capitalize6(word) {
return word
.split('')
.map((letter, index) => index ? letter.toLowerCase() : letter.toUpperCase())
.join('')
}