Advertisement
makispaiktis

Iterators with return values

Oct 19th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Data
  2. const cities = ['Orlando', 'Dubai', 'Edinburgh', 'Chennai', 'Accra', 'Denver', 'Eskisehir', 'Medellin', 'Yokohama'];
  3. const nums = [1, 50, 75, 200, 350, 525, 1000];
  4.  
  5. //  1. forEach: returns undefined
  6. cities.forEach(city => console.log('Have you visited ' + city + '?'));
  7. // 2. Filter: returns a new array
  8. const longCities = cities.filter(city => city.length > 7);
  9. // 3. reduce: returns a single value
  10. const word = cities.reduce((acc, currVal) => {
  11.   return acc + currVal[0]
  12. }, "C");
  13. console.log(word)
  14. // 4. map: returns a new array
  15. const smallerNums = nums.map(num => num - 5);
  16. console.log(smallerNums);
  17. // 5. returns a boolean value
  18. nums.every(num => num < 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement