-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
146 lines (111 loc) · 3.71 KB
/
demo.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
137
138
139
140
141
142
143
144
145
146
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}
];
// Companies functions
// const retailCompanies = companies.filter(function(company) {
// if (company.category === 'Retail') {
// return true;
// }
// });
// for(let i = 0; i < companies.length; i++) {
// console.log(companies[i]);
// }
// Foreach
// companies.forEach(function(company) {
// console.log(company);
// });
const retailCompanies = companies.filter(company => company.category === 'Technology');
// console.log(retailCompanies);
// filter multiple years
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
// console.log(eightiesCompanies);
// Decade old companies
const lastedTenYears = companies.filter(company => (company.end - company.start >10));
// console.log(lastedTenYears);
// Array of company names
// const companyNames = companies.map(function(company){
// return company.name;
// });
// Long way
// const companyNames = companies.map(function(company){
// return `${company.name} [${company.start} - ${company.end}]`;
// });
// Short way
const companyNames = companies.map(company =>
`${company.name} [${company.start} - ${company.end}]`);
// console.log(companyNames);
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
// Filtering
// let canDrink = [];
// for(let i = 0; i < ages.length; i++) {
// if (ages[i] >= 21) {
// canDrink.push(ages[i]);
// }
// }
// The long way - filtering ages 21 or older
// const canDrink = ages.filter(function(age) {
// if (age >= 21) {
// return true;
// }
// });
// short way - filtering ages 21 or older
const canDrink = ages.filter(age => age >= 21);
// console.log(canDrink);
// const agesSquare = ages.map(age => Math.sqrt(age));
// const agesTimesTwo = ages.map(age => age * 2);
const ageMap = ages
.map(age => Math.sqrt(age))
.map(age => age * 2);
// console.log(ageMap);
// Sorting - by start year
// Long way
// 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));
// console.log(sortedCompanies);
// ascending
const sortAgesAsc = ages.sort((a, b) => a - b);
// Descending
const sortAgesDesc = ages.sort((a, b) => b - a);
// console.log(sortAgesAsc);
// REDUCE
// let ageSum = 0;
// with a for loop
// for(let i = 0; i < ages.length; i++) {
// ageSum += ages[i];
// }
// Long way
// const ageSum = ages.reduce(function(total, age) {
// return total + age;
// }, 0);
// Short way
const ageSum = ages.reduce((total, age) => total + age, 0);
// console.log(ageSum);
// Total years companies
// Long way
// const totalYears = companies.reduce(function(total, company) {
// return total + (company.end - company.start);
// }, 0);
// Short way
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
// console.log(totalYears);
// Combining
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);