-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (68 loc) · 2.26 KB
/
main.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
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 1987},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Automobile", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 2000, end: 2001},
{name: "Company Five", category: "Technology", start: 2005, end: 2018},
{name: "Company Six", category: "Finance", start: 1975, end: 1990},
{name: "Company Seven", category: "Automobile", start: 2004, end: 2010},
{name: "Company Eight", category: "Technology", start: 1986, end: 2001},
{name: "Company Nine", category: "Retail", start: 1992, end: 2003},
];
const ages = [23, 65, 13, 22, 89, 25, 18, 9, 13];
// ==========
// FOR EACH LOOP
companies.forEach(company => {
console.log(company.category);
});
// ==========
// FOR LOOP
let canDrink = [];
for(let i = 0; i < ages.length; i++) {
if(ages[i] >= 21) {
canDrink.push(ages[i]);
}
}
console.log(canDrink);
// ==========
// FILTER
let canDrink = ages.filter(age => {
if(age >= 21) {
return true;
}
});
let canDrink = ages.filter(age => age >=21);
console.log(canDrink);
let retailCompanies = companies.filter(company => company.category === "Retail");
console.log(retailCompanies);
let eightiesCompanies = companies.filter(company => company.start >= 1980 && company.end <= 1989);
console.log(eightiesCompanies);
const lastedTenYears = companies.filter(company => company.end - company.start >= 10);
console.log(lastedTenYears);
// ==========
// MAP
let companyNames = companies.map(company => company.name);
console.log(companyNames);
// ==========
// SORT
let sortedCompanies = companies.sort((a, b) => a.start > b.start ? 1 : -1);
console.log(sortedCompanies);
let sortedAges = ages.sort((a, b) => b - a);
console.log(sortedAges);
// ==========
// REDUCE
let ageSum = 0;
for(let i = 0; i < ages.length; i++) {
ageSum += ages[i];
}
console.log(ageSum);
let ageSum = ages.reduce((total, age) => total + age, 0);
console.log(ageSum);
let totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
console.log(totalYears);
let combined = ages
.map(age => age * 2)
.filter(age => age >= 40)
.sort((a, b) => a - b)
.reduce((a, b) => a + b, 0);
console.log(combined);