Reduce

Run Settings
LanguageJavaScript
Language Version
Run Command
const companies=[ { name: "company one", category: "f", start:1981, end: 2004}, { name: "company two", category: "retail", start:1991, end: 2024}, { name: "company three", category: "f", start:2000, end: 2024}, { name: "company four", category: "retail", start:1881, end: 2004}, { name: "company five", category: "sales", start:2001, end: 2025}, { name: "company six", category: "sales", start:1981, end: 2014}, { name: "company seven", category: "f", start:1981, end: 2004}, { name: "company eight", category: "reails", start:2020, end: 2025}, { name: "company nine", category: "sales", start:1981, end: 2024}, { name: "company ten", category: "f", start:1991, end: 2014}, ]; const ages=[33,12,36,23,77,65,48,33,26,6,15,44]; // for loop <- for(let i =0; i <ages.length; i++){ console.log(ages[i]) } //for each companies.forEach((company)=> { company.start++; }); console.log(companies); console.log("------for each + if then else----- "); //for each companies.forEach((company)=> { company.start = company.start >2000? company.start++ : company.start; }); console.log("------for each + if then else----- "); console.log(companies); companies.forEach((company, index, arr)=> { console.log(company, index, arr); }); console.log(companies); console.log("-----for loop company 7 + company 9 ---- "); total =0; companies.forEach((company, index, arr) =>{ if(index < arr.length -2){ total =company.start + arr[index+2].start } }); console.log(total); console.log("------total age ----- "); total =0; ages.forEach((age)=> total +=age); console.log(total); console.log("------filter age ----- "); let kids = ages.filter(age=> age<= 19); console.log(kids); console.log(ages); console.log("------filter age old way----- "); kids =[]; for(let i =0; i <ages.length; i++){ if(ages[i] <=18) { kids.push(ages[i]);} } console.log(kids); console.log("------filter companies----- "); const result = companies.filter((company) => { return company.start >= 2000 && company.category == "f" }); console.log(result); console.log("------filter companies by year----- "); const result2 = companies.filter((company) => { return company.start >= 2014 && company.end >=2014 && company.end<=2026 }); console.log(result2); console.log("----map ex1----- "); const companyNames= companies.map((company)=> company.name); console.log(companyNames); console.log("----map ex2 ${}----- "); const testmap = companies.map((company)=>{ return `${company.name} [${company.start}-${company.end}]` }); console.log(testmap); console.log("----map 2 times----- "); const ageMap= ages.map(age=> Math.sqrt(age)).map(age=> age*2); console.log(ageMap); console.log("----Array function -sorting---- "); const sortedCompanies = companies.sort((c1,c2) => c2.start > c1.start ? 1 : -1); console.log(sortedCompanies); console.log("----Array function -sorting 2---- "); const sortedCompanies1 = companies.sort((c3,c2) => c3.start > c2.start ? 1 : -1); console.log(sortedCompanies1); // reducer console.log("-------reducer---------"); a = [5,8,6, 10,9]; const maxNum = a.reduce((max, current) => (current > max ? current : max), a[0]); console.log(maxNum); console.log("-------reducer. max---------"); const arr = [5, 7, 9, 11, 51, 43, 55, 6, 87, 12]; const maxValue = (a, b) => (a > b ? a : b); function seqeMax(arr) { let result = arr[0]; for (let element of arr) { result = maxValue(result, element); } return result; } console.log(seqeMax(arr)); // Output: 87 console.log("-------reducer. min---------"); const minValue = (a, b) => (a < b ? a : b); function seqeMin(arr) { let result = arr[0]; for (let element of arr) { result = minValue(result, element); } return result; } console.log(seqeMin(arr)); // Output: 5 console.log("-------design pattern ==> reduce function---------"); const sum = (a, b) => (a+b); function seqe3(fn, arr, init) { let result = init; for (let element of arr) { result = fn(result, element); } return result; } console.log(seqe3(sum, arr, 0)); console.log(seqe3(maxValue, arr, -99999)); console.log(seqe3(minValue, arr, 999999)); console.log("-------design pattern ==> reduce function---------"); let agesum =0; for(let i =0; i <ages.length;i++){ agesum = agesum +ages[i]; } console.log(agesum); //ex 1 console.log("-------reducing function way. totla =0---------"); const ageSum = ages.reduce((total,age)=>{ return total + age; }, 0); console.log(ageSum); // ex2 const totalYears = companies.reduce((total, company,index)=>{ console.log(index); return company.end >= 2025 ? total (company.end - company.start) :total; },0); console.log(totalYears);
Editor Settings
Theme
Key bindings
Full width
Lines