Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. //forEach()
  2. // for loop
  3. for (let i = 0; i < companies.length; i++) {
  4. console.log(companies[i]);
  5. }
  6.  
  7. // forEach()
  8. companies.forEach(function(company) {
  9. console.log(company);
  10. });
  11. // forEach() Arrow Function
  12. companies.forEach(company => console.log(company))
  13.  
  14.  
  15. // filter()
  16. // get 21 and older
  17. /* let canDrink = [];
  18. for (let i = 0; i < ages.length; i++) {
  19. if (ages[i] >= 21) {
  20. canDrink.push(ages[i]);
  21. }
  22. } */
  23.  
  24. /* const canDrink = ages.filter(function(age) {
  25. if (age >= 21) {
  26. return true;
  27. }
  28. }); */
  29.  
  30. const canDrink = ages.filter(age => age >= 21);
  31.  
  32. // map()
  33. // Create array of company names
  34. const companyNames = companies.map(function(company) {
  35. return company.name;
  36. });
  37. console.log(companyNames);
  38.  
  39. const agesSquare = ages.map(age => Math.sqrt(age));
  40. const agesTimesTwo = ages.map(age => age * 2);
  41.  
  42. const ageMap = ages.map(age => Math.sqrt(age)).map(age => age * 2);
  43.  
  44. // sort()
  45. // sort companies by start Year
  46. const sortedCompanies = companies.sort(function(c1, c2) {
  47. if (c1.start > c2.start) {
  48. return 1;
  49. } else {
  50. }
  51. return -1;
  52. });
  53.  
  54. const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
  55.  
  56. // Sort Ages
  57. const sortAges = ages.sort((a, b) => a - b);
  58.  
  59.  
  60. // Combine Methods
  61.  
  62. const combined = ages
  63. .map(age => age * 2)
  64. .filter(age => age >= 40)
  65. .sort((a, b) => a - b)
  66. .reduce((a, b) => a + b, 0);
  67.  
  68. console.log(combined);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement