-
Notifications
You must be signed in to change notification settings - Fork 91
/
Letter Capitalize
34 lines (30 loc) · 2.15 KB
/
Letter Capitalize
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
/***************************************************************************************
* *
* CODERBYTE BEGINNER CHALLENGE *
* *
* Letter Capitalize *
* Using the JavaScript language, have the function LetterCapitalize(str) take the *
* str parameter being passed and capitalize the first letter of each word. Words *
* will be separated by only one space. *
* *
* SOLUTION *
* I am going to convert the string to an array based on the space character that *
* separates each word. Then I am going to loop through each word in the array and *
* capitalize the first letter of the word. I am going to convert array back to a *
* string to return as the answer.
* *
* Steps for solution *
* 1) Convert string to arry of words by splitting on space. *
* 2) Loop thru each word in the array and convert first letter to uppercase *
* using the charAt(0) value and then appending the remainder of the word *
* using the slice() function *
* 3) Convert array to string with join() function. *
* *
***************************************************************************************/
function LetterCapitalize(str) {
var arr = str.split(" ");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
return arr.join(" ");
}