Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // Array.prototype.join();
  2.  
  3. var name = ['Shane', 'Osbourne'];
  4.  
  5. // give all the values back as a string and I provide the separator in between
  6. console.log(names.join(' ')); // Shane Osbourne with a space in between
  7. console.log(names.join('-')); // Shane-Osbourne
  8. console.log(names.join('')); // ShaneOsbourne
  9. console.log(names.join()); // when not providing any arg at all, you get the values separated with a comma -> Shane,Osbourne
  10.  
  11. // practical USE CASE:
  12. /*
  13. 1.Lets say you have a command line program and you want to provide a help screen to users;
  14. */
  15.  
  16. // store each line of the help text in an array
  17. var help = [
  18. 'Usage',
  19. ' foo-app <input>'
  20. ]
  21. // check if the user has run the 'help' command by looking at the third argument available tools
  22. if(process.argv[2] === 'help'){
  23. console.log(help.join('\n')); // takes every item in the help array, puts a new line in between each one and prints the result
  24. }; // prints the values from the help array in the terminal (when users run help in the terminal):
  25. /*
  26. Usage
  27. foo-app <input>
  28. */
  29.  
  30. /*
  31. 2. You want to Upper Case the first letter of each word in a String
  32. */
  33. var name = 'shane osbourne';
  34.  
  35. var upper = name.split(' ') // [shane, osbourne]
  36. .map(x => x.charAt(0).toUpperCase() + x.slice(1)) // produces another array: [Shane, Osbourne]
  37. .join(' '); // creates a string: 'Shane Osbourne'
  38.  
  39. console.log(upper); // 'Shane Osbourne'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement