1var array = [36, 25, 6, 15];
2
3array.reduce(function(accumulator, currentValue) {
4 return accumulator + currentValue;
5}, 0); // 36 + 25 + 6 + 15 = 82
1let newArray = array.reduce( (acc, element, index, originalArray) => {
2 // return acc += element
3}, initialValue)
1function reduce(array, func, seed) {
2 let previousResult;
3 if (seed === undefined) {
4 previousResult = array[0];
5 for (let i = 1; i < array.length; i++) {
6 previousResult = func(previousResult, array[i], i, array);
7 }
8 console.log(previousResult);
9 } else {
10 previousResult = seed;
11 each(array, function(e, i, a) {
12 previousResult = func(previousResult, e, i, array);
13 });
14 }
15 return previousResult;
16};
1const developers = [
2 {
3 name: "Eliza",
4 experience: 7,
5 role: "manager"
6 },
7 {
8 name: "Manuel",
9 experience: 2,
10 role: "developer"
11 },
12 {
13 name: "Kim",
14 experience: 5,
15 role: "developer"
16 }
17];
18
19function calculateAverage(total, years, index, array) {
20 total += years;
21 return index === array.length-1 ? total/array.length: total
22}
23
24const average = developers.map(dev => dev.experience).reduce(calculateAverage);