-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropcaps.js
39 lines (33 loc) · 1.15 KB
/
dropcaps.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
// Dropcaps
/*
DropCaps means that the first letter of the starting word of the paragraph should be in caps and the remaining lowercase, just like you see in the newspaper.
But for a change, let"s do that for each and every word of the given String. Your task is to capitalize every word that has length greater than 2, leaving smaller words as they are.
should work also on Leading and Trailing Spaces and caps.
"apple" => "Apple"
"apple of banana" => "Apple of Banana"
"one space" => "One Space"
" space WALK " => " Space Walk "
*/
const dropCap = n => {
let arr = n.split(' ');
for(let i = 0; i < arr.length; i++){
if(arr[i].length > 2){
arr[i] = arr[i].toLowerCase().split('')
arr[i][0] = arr[i][0].toUpperCase()
arr[i] = arr[i].join('')
}
}
return arr.join(' ')
}
// passes
// refactored
const dropCapTwo = n => {
let arr = n.split(' ');
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > 2) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1).toLowerCase();
}
}
return arr.join(' ');
}
// passes tests