-
Notifications
You must be signed in to change notification settings - Fork 0
/
higherOrderFunctions.js
137 lines (94 loc) · 3.41 KB
/
higherOrderFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*PATTERN:
SourceArrey.forEach/filter/some/every/map/sort((arreyItem)=> criteria/operations)
SoureArrey.reduce((resultVariable,arreyItem, indexOfItem) => resultValue operator arreyItem.value, initialValue)*/
const companies= [
{name: "Company One", category: "Finance", start: 1981, end: 2004},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
ages.reduce()
// for(let i = 0; i < companies.length; i++) {
// console.log(companies[i]);
// }
// forEach
// companies.forEach(function(company) {
// console.log(company.name);
// });
// filter
// Get 21 and older
// let canDrink = [];
// for(let i = 0; i < ages.length; i++) {
// if(ages[i] >= 21) {
// canDrink.push(ages[i]);
// }
// }
// const canDrink = ages.filter(function(age) {
// if(age >= 21) {
// return true;
// }
// });
const canDrink = ages.filter(age => age >= 21);
// Filter retail companies
// const retailCompanies = companies.filter(function(company) {
// if(company.category === 'Retail') {
// return true;
// }
// });
const retailCompanies = companies.filter(company => company.category === 'Retail');
// Get 80s companies
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
// Get companies that lasted 10 years or more
const lastedTenYears = companies.filter(company => (company.end - company.start >= 10));
// map
// Create array of company names
// const companyNames = companies.map(function(company) {
// return company.name;
// });
// const testMap = companies.map(function(company) {
// return `${company.name} [${company.start} - ${company.end}]`;
// });
// const testMap = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
// const ageMap = ages
// .map(age => Math.sqrt(age))
// .map(age => age * 2);
// sort
// Sort companies by start year
// const sortedCompanies = companies.sort(function(c1, c2) {
// if(c1.start > c2.start) {
// return 1;
// } else {
// return -1;
// }
// });
// const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
// Sort ages
// const sortAges = ages.sort((a, b) => a - b);
// console.log(sortAges);
// reduce
// let ageSum = 0;
// for(let i = 0; i < ages.length; i++) {
// ageSum += ages[i];
// }
// const ageSum = ages.reduce(function(total, age) {
// return total + age;
// }, 0);
// const ageSum = ages.reduce((total, age) => total + age, 0);
// Get total years for all companies
// const totalYears = companies.reduce(function(total, company) {
// return total + (company.end - company.start);
// }, 0);
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
// Combine Methods
const combined = ages
.map(age => age * 2)
.filter(age => age >= 40)
.sort((a, b) => a - b)
.reduce((a, b) => a + b, 0);
console.log(combined);