Guest User

Untitled

a guest
Feb 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. /*
  2. Create a function that accepts a string, converts the string to title case, and
  3. returns the result. For title case, the first letter of each word is capitalized.
  4.  
  5. capitalize('hello world'); //Outputs: 'Hello World'
  6. captialize('the year of the hare'); //Outputs: 'The Year Of The Hare';
  7.  
  8. Pseudo code:
  9. 1. Split the string into array using str.split(' ');
  10. 2. Map over the elements in the array. Title case each word using helper function.
  11. 3. Join the elements of the newly defined array separated by space.
  12. 4. Return the result.
  13. */
  14.  
  15. function capitalize(str) {
  16. return str.split(' ').map(word => titleCase(word)).join(' ');
  17. }
  18.  
  19. //Helper function to capitalize a single word.
  20. function titleCase(word) {
  21. return word[0].toUpperCase() + word.slice(1);
  22. }
  23.  
  24. //Try it
  25. console.log(capitalize('arto paasilinna'));
Add Comment
Please, Sign In to add comment