tobym76

Multiple-functions-chained

Nov 24th, 2018
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const cities = ['Torino', 'London', 'Dubai', 'London', 'Edinburgh'];
  2.  
  3. const friendlyMessage = cities
  4.   .filter(city => haveILivedIn(city))
  5.   .map(city => shout(city))
  6.   .reduce((message, city) => {
  7.     return addBulletPoint(message, city);
  8.   }, 'In my life I lived in: ');
  9.  
  10. console.log(friendlyMessage);
  11. //In my life I lived in:
  12. // - TORINO
  13. // - LONDON
  14. // - DUBAI
  15. // - LONDON
  16.  
  17. function shout(string) {
  18.   return string.toUpperCase();
  19. }
  20. function haveILivedIn(city) {
  21.   return city !== 'Edinburgh';
  22. }
  23. function addBulletPoint(string, item) {
  24.   return (string += `\n - ${item}`);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment