const companies = [
{name: "company One", category: "Finance", start: 1981, end: 2004},
{name: "company Two", category: "Retail", start: 1982, end: 2005},
{name: "company Three", category: "Retail", start: 1983, end: 2006},
{name: "company Four", category: "Finance", start: 1985, end: 2007},
{name: "company Five", category: "Auto", start: 1986, end: 2008},
{name: "company Six", category: "Finance", start: 1987, end: 2009},
{name: "company Seven", category: "Auto", start: 1988, end: 2010},
{name: "company Eight", category: "Retail", start: 1989, end: 2011},
{name: "company Nine", category: "Retail", start: 1990, end: 2012},
{name: "company Ten", category: "Finance", start: 1961, end: 2013},
];
const ages = [33,12,55,23,11,77,43,24,89,41];
// normal for loop eg. 1
// for (let i=0; i <companies.length; i++){
// console.log("~eg.1~",companies[i].start);
// }
// foreach >> eg. 2 [foreach example]
// companies.forEach(company=> console.log("~eg.2~",company.start));
// eg. 3
// let total = 0;
// ages.forEach(age=>total = total + age);
// console.log("~eg.3~",ages);
let total = 0;
ages.forEach(age=>total = total + age);
// // eg. 4 loop ages and get each element value add to total
// ages.forEach((age,index,ages)=>ages[index]= ++age);
// // eg. 5 loop ages with optional index, ages parameters and update ages content
console.log("~eg.5~",ages);
// eg. 6 sort out more then "38"
let adult = [];
for (let i=0; i<ages.length; i++){
if (ages[i] >= 38) {
adult.push("~eg.6~",ages[i]);
}
}
console.log(adult);
//array fliter e.g 7
let adult1 = ages.filter(age => {
if (age < 38) {
return true;
}
});
console.log("~eg.7~", adult1);
//eg. 8, another expression as eg.7
adult1 = ages.filter(age=> age <28);
console.log("~eg.8~", adult1);
//eg. 9 sort out "retail" category
const retailCompanies=companies.filter(company=>company.category=="Retail");
console.log("eg.9", retailCompanies);
//eg. 10 sourt out "over XX years"
const tenYears=companies.filter(company=>company.end-company.start >=30 );
console.log("eg.10", tenYears);
//eg. 11
const eightly=companies.filter(company=>company.start >= 1980 && company.start <1990);
console.log("~eg.11~", eightly);
//eg. 12 Map ?? error?
const testMap = companies.map(company=> `${company.name} [${company.start} - ${company.end}]`);
console.log ("~eg.12~", testMap);
// eg. 13
const ageMap = ages.map(age=>Math.sqrt(age)).map(age=>age *2);
console.log("~eg.13~", ageMap);
// eg. 14 how [reducer] work in JS
max_value = (a,b) => a>b ? a:b;
function max_sequence(a) {
let result = a[0];
for (let e of a) result = max_value(result,e);
return result;
}
console.log("~eg.14~", max_sequence(ages));
//e.g 15
// min_value = (a,b) => a<b ? a:b;
// function min_sequence(a) {
// let result = a[0];
// for (let e of a) result = min_value(result,const testMap = companies.map(company=> `${company.name} [${company.start} - ${company.end}]`);
// return result
// }
// console.log("~eg.15~", min_sequence(ages));
// eg. 16
function reduce(a, func) {
let result = a[0];
for (let e of a) result = func(result, e);
return result
}
console.log("~eg.16~", reduce(ages, max_value));
console.log("~eg.16~", reduce(ages, min_value));
//eg. 17
function reduce1(a, func) {
let result = init;
for (let e of a) result = func(result, e);
return result
}
console.log("~eg.17~", reduce1(ages,max_value, -99));
// eg. 18 js array reducer
const ageSum = ages.reduce((total,age)=>{return total + age},0);
console.log("~eg.18~", ageSum);
// eg. 19
const totalCompany = companies.reduce((total, company)=> {return total + company.end - company.start},0);
console.log("~eg.19~", totalCompany);