Skip to content

Commit a08fb45

Browse files
committed
capitalize first letter in each word in a given string;
1 parent ae63e44 commit a08fb45

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// function accepts a string containing words and returns a string which
2+
// has every word capitalized (i.e. first letter is a capital)
3+
4+
// O(n)
5+
function capitalizeFirstLetter_JS_Way(inputString: string): string {
6+
const words: string[] = inputString.split(' ');
7+
8+
const capitalizedWords: string[] = words.map(
9+
(word) => word.slice(0, 1).toUpperCase() + word.slice(1),
10+
);
11+
12+
return capitalizedWords.join(' ');
13+
}
14+
15+
// should return: I Am A String
16+
console.log(capitalizeFirstLetter_JS_Way('i am a string'));
17+
18+
// should return: I Am A String With Comma, And Stuff!
19+
console.log(capitalizeFirstLetter_JS_Way('i am a string with comma, and stuff!'));

0 commit comments

Comments
 (0)