const companies = [
{name :"company one", category :'finance', start : 1991, end : 2024},
{name :"company two", category : "finance", start : 2004, end : 2016},
{name :"company three", category : "finance", start : 2003, end : 2015},
{name :"company four", category : "finance", start : 2004, end : 2014},
{name :"company five", category : "finance", start : 2005, end : 2007},
{name :"company six", category : "finance", start : 2006, end : 2008},
{name :"company seven", category : "finance", start : 2007, end : 2009},
{name :"company eight", category : "finance", start : 2008, end : 2019},
{name :"company nine", category : "finance", start : 2010, end : 2012},
{name :"company ten", category : "finance", start : 1981, end : 2004}
];
const ages = [33,12,56,23,77,65,48,33,26,6,15,44];
for (let i = 0; i < ages.length; i++) {
console.log(ages[i]);
}
companies.forEach((company)=> {
console.log(company.start);
company.start = company.start > 2000 ?
++company.start : company.start;
});
total = 0;
companies.forEach((company,index,arr) => {
if (index < arr.length - 2) {
total += company.start + arr[index + 2].start
}
});
console.log(total);
total = 0;
ages.forEach((age)=> total += age)
let kids = ages.filter(age=> age <= 18);
console.log(ages,kids);
kied = []
// for (let i = 0; i < ages.length; i++) {
// if (ages[i] <=18) {
// kieds.push(ages[i]);
// }
// }
//
console.log(kids);
result = companies.filter((company)=> {
// return company.start >= 2000 && company.category == 'finance'
return company.start >= 2000 && company.end >= 2014 && company.end <=2020
});
console.log(result);
const companyNames = companies.map((company)=> company.name);
console.log(companyNames,companies);
const testmap = companies.map((company)=> {
return `${company.name} [${company.start} - ${company.end}]`
});
console.log(testmap);
const ageMap = ages.map(age => Math.sqrt(age)).map(age=> age * 2);
console.log(ageMap);
const sortedCompanies = companies.sort((c1,c2) => c1.start > c2.start ? 1: -1);
console.log(sortedCompanies);
// reducer
a = [5,8,6,10,9];
maxValue = (a,b) => (a > b ? a : b);
function seq(a) {
let result = a[0];
for (element of a){
result = maxValue(result,element)}
return result;
}
console.log(seq(a));
maxValue = (a,b) => (a > b ? a : b);
minValue = (a,b) => (a < b ? a : b);
function seq3(fn,a) {
let result = a[0];
for (element of a) {
result = fn(result,element)}
return result;
}
// console.log(seql(a));
console.log(seq3(maxValue,a));
console.log(seq3(minValue,a));
maxValue = (a,b) => (a > b ? a : b);
minValue = (a,b) => (a < b ? a : b);
sum = (a,b) => a + b;
function seq3(fn,a,init) {
let result = init;
for (element of a) {
result = fn(result,element)}
return result;
}
// const a = [5,8,6,10,9];
console.log(seq3(maxValue,a), -999999);
console.log(seq3(minValue,a), 99999);
console.log(seq3(sum,a,0));
console.log(seq3((a,b)=>a+b, a, 0));