Skip to content

Commit 1572ec6

Browse files
committed
title case 🗼
1 parent 982a5ff commit 1572ec6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
2121
- [8. Truncate a String](#8-truncate-a-string)
2222
- [9. Finders Keepers](#9-finders-keepers)
2323
- [10. Boo who](#10-boo-who)
24+
- [11. Title Case a Sentence](#11-title-case-a-sentence)
2425

2526
## Basic Algorithm Scripting
2627

@@ -343,3 +344,44 @@ function booWho(bool) {
343344

344345
booWho(null);
345346
```
347+
348+
### 11. Title Case a Sentence
349+
350+
_Difficulty: Beginner_
351+
352+
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
353+
354+
For the purpose of this exercise, you should also capitalize connecting words like the and of.
355+
356+
---
357+
358+
#### Solution 1
359+
360+
```js
361+
function titleCase(str) {
362+
let convertToArray = str.toLowerCase().split(" ");
363+
364+
for (let i = 0; i < convertToArray.length; i++) {
365+
convertToArray[i] =
366+
convertToArray[i].charAt(0).toUpperCase() + convertToArray[i].slice(1);
367+
}
368+
return convertToArray.join(" ");
369+
}
370+
371+
titleCase("I'm a little tea pot");
372+
```
373+
374+
#### Solution 2
375+
376+
```js
377+
function titleCase(str) {
378+
let convertToArray = str.toLowerCase().split(" ");
379+
return convertToArray
380+
.map((item) => {
381+
return item.replace(item.charAt(0), item.charAt(0).toUpperCase());
382+
})
383+
.join(" ");
384+
}
385+
386+
titleCase("I'm a little tea pot");
387+
```

0 commit comments

Comments
 (0)