-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
112 lines (83 loc) · 2.91 KB
/
app.js
File metadata and controls
112 lines (83 loc) · 2.91 KB
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
const companies = [
{name: "Company One", category: "Finance", start: 1981, end: 2003},
{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];
// companies.forEach(function(company) {
// console.log(company.name);
// });
//filter age > 21 #old style for loop
// let canDrink = [];
// for (let i = 0; i < ages.length; i++) {
// if (ages[i] > 21) {
// canDrink.push(ages[i]);
// }
// }
//new style filter
// const canDrink = (ages.filter(function(age) {
// return age > 21;
// }));
//or ES6
const canDrink = ages.filter(age => age > 21);
console.log(canDrink);
//filter company with category = "Retail"
const RetailCompanies = companies.filter(company => company.category === "Retail");
// console.log(RetailCompanies)
//filter company with category = start > 1979 and < 1990
const eightiesCompany = companies.filter(company => company.start > 1979 && company.start < 1990);
// console.log(eightiesCompany);
//filter company with category = end - start > 10
const tenYearsCompany = companies.filter(company => company.end - company.start >= 10);
// console.log(tenYearsCompany);
//map
//map company to array of names with start and end years
const companyNames = companies.map(company => {
return [company.name, company.start, company.end];
})
// console.log(companyNames);
//map ages to array of squareroots
const agesSquared = ages.map(age => {
return Math.sqrt(age);
})
// console.log(agesSquared);
//map ages to array of squareroots and map age to array of ages * 2
const agesSquaredTimesTwo = ages.map(age => {
return Math.sqrt(age) * 2;
})
// console.log(agesSquaredTimesTwo);
//or
const agesSquared1 = ages
.map(age => Math.sqrt(age))
.map(age => age * 2);
// console.log(agesSquared1);
//sorting
// sort company by start year
const sortedCompany = companies.sort((a, b) => {
return a.start - b.start;
})
// console.log(sortedCompany);
//sort ages by age
const sortedAges = ages.sort((a, b) => {
return a - b;
})
// console.log(sortedAg es);
//reduce
//reduce age sum
const sum = ages.reduce((a, b) => {
return a + b;
})
// console.log(sum);
//reduce total years for all comapnies
const totalYears = companies.reduce((total, company) =>
total + ( company.end - company.start), 0);
// const sumCompany = companies.reduce((a , b) => {
// return a.start + b.end;
// })
console.log(totalYears);