File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
21
21
- [ 8. Truncate a String] ( #8-truncate-a-string )
22
22
- [ 9. Finders Keepers] ( #9-finders-keepers )
23
23
- [ 10. Boo who] ( #10-boo-who )
24
+ - [ 11. Title Case a Sentence] ( #11-title-case-a-sentence )
24
25
25
26
## Basic Algorithm Scripting
26
27
@@ -343,3 +344,44 @@ function booWho(bool) {
343
344
344
345
booWho (null );
345
346
```
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
+ ```
You can’t perform that action at this time.
0 commit comments