Skip to content

Commit e90c710

Browse files
committed
JavaScript High Order Functions Map, reduce, filter, sort, forEach
1 parent 3a12e21 commit e90c710

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=h2, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<h2>JAVASCIPT</h2>
11+
12+
<script src="main.js"></script>
13+
</body>
14+
</html>
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
const companies= [
2+
{name: "Company One", category: "Finance", start: 1981, end: 2004},
3+
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
4+
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
5+
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
6+
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
7+
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
8+
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
9+
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
10+
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
11+
];
12+
13+
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
14+
15+
// forLoop
16+
// for(let i = 0; i < companies.length; i++){
17+
// console.log(companies[i]);
18+
// }
19+
20+
// ForEach Loop
21+
// companies.forEach(function(company){
22+
// console.log(company);
23+
// });
24+
25+
// Filter
26+
// Get 21 and Older
27+
28+
// let canDrink = [];
29+
// for(let i = 0; i < ages.length; i++){
30+
// if(ages[i]>=21){
31+
// canDrink.push(ages[i]);
32+
// }
33+
// }
34+
// console.log(`Traditional Loop : ${canDrink}`);
35+
36+
// const canDrink1 = ages.filter(function(age){
37+
// if(age>=21){
38+
// return true;
39+
// }
40+
// })
41+
42+
// console.log(`Filter condition : ${canDrink1}`);
43+
44+
// const canDrink2 = ages.filter(age => age >= 21);
45+
// console.log(`ES6 syntax method : ${canDrink2}`);
46+
47+
// Filter retail companies
48+
49+
// const retailCompanies = companies.filter(function(company){
50+
// if(company.category === 'Retail'){
51+
// return true;
52+
// }
53+
// });
54+
55+
// console.log(`Using filter function :`);
56+
// console.log(retailCompanies);
57+
58+
// const retailCompanies2 = companies.filter(company => company.category === 'Retail');
59+
// console.log(`Using ES6 syntax : ${retailCompanies2}`);
60+
61+
// Get 80s companies
62+
63+
// const eightiesCompanies = companies.filter(function(company){
64+
// if(company.start >= 1980 && company.start < 1990){
65+
// return true;
66+
// }
67+
// });
68+
69+
// console.log(`COmpanies established in 80's :`);
70+
// console.log(eightiesCompanies);
71+
72+
// const eightiesComapnies1 = companies.filter(company => company.start>=1980 && company.start < 1990);
73+
// console.log('Using ES6 filter function : ');
74+
// console.log(eightiesComapnies1);
75+
76+
// Lasted 10 years and more
77+
// const lastedTenYears = companies.filter(function(company){
78+
// if(company.end - company.start >=10){
79+
// return true;
80+
// }
81+
// });
82+
83+
// console.log('Lasted 10 years : ');
84+
// console.log(lastedTenYears);
85+
86+
// const lastedTenYears1 = companies.filter(company => company.end - company.start >= 10);
87+
// console.log('ES6 function lasted for more then 10 years : ');
88+
// console.log(lastedTenYears1);
89+
90+
// Difference between const, let and var
91+
// function getClothing(isCold) {
92+
// if (isCold) {
93+
// const freezing = 'Grab a jacket!';
94+
// } else {
95+
// const hot = 'It’s a shorts kind of day.';
96+
// console.log(freezing);
97+
// }
98+
// }
99+
100+
// getClothing(false);
101+
102+
// MAP
103+
104+
// Create array of company names
105+
106+
// const companyNamesMap = companies.map(function(company){
107+
// return `${company.name} [${company.start} - ${company.end}]`;
108+
// });
109+
110+
// console.log(companyNamesMap);
111+
112+
// // ES6 Version
113+
// console.log('--------ES6Version----------');
114+
// const companyNamesES6 = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
115+
// console.log(companyNamesES6);
116+
117+
118+
// const ageMap = ages
119+
// .map(age => age*age)
120+
// .map(age => Math.sqrt(age));
121+
122+
// console.log(ageMap);
123+
124+
// SORT
125+
126+
// SORT COMPANIES BY START YEAR
127+
// const sortedCompanies = companies.sort(function(c1,c2){
128+
// // return c1.start > c2.start;
129+
130+
// if(c1.start > c2.start){
131+
// return 1;
132+
// }else{
133+
// return -1;
134+
// }
135+
// });
136+
137+
// console.log(sortedCompanies);
138+
139+
// // ES6 Version
140+
// console.log('ES6 Version');
141+
// const sortedCompaniesES6 = companies.sort((c1,c2) => c1.start>c2.start ? 1 : -1);
142+
// console.log(sortedCompaniesES6);
143+
144+
// Sort Ages
145+
146+
// const sortAges = ages.sort(function(a,b){
147+
// // return a>b;
148+
// if(a>b){
149+
// return 1;
150+
// }else{
151+
// return -1;
152+
// }
153+
// });
154+
// console.log(sortAges);
155+
156+
// //ES6 Version
157+
158+
// const sortAgesES6 = ages.sort((a,b) => a>b?1:-1);
159+
// console.log(sortAgesES6);
160+
161+
// REDUCE
162+
163+
// let ageSum = 0;
164+
// for(let i = 0; i < ages.length; i++){
165+
// ageSum += ages[i];
166+
// }
167+
168+
// const ageSum = ages.reduce(function(total,age){
169+
// return total + age;
170+
// },0);
171+
172+
// const ageSum = ages.reduce((total,age) => total + age,0);
173+
174+
// console.log(ageSum);
175+
176+
// Get total years for all companies
177+
178+
// const totalYears = companies.reduce(function(total,company){
179+
// return total + (company.end - company.start);
180+
// },0);
181+
182+
183+
// const totalYears = companies.reduce((total,company) => total + (company.end-company.start),0);
184+
185+
// console.log(totalYears);
186+
187+
// Combine Methods
188+
189+
// const combined = ages
190+
// .map(age => age*2)
191+
// .filter(age => age >= 40)
192+
// .sort((a,b) => a-b)
193+
// .reduce((a,b) => a+b,0)
194+
195+
// console.log(combined);

0 commit comments

Comments
 (0)