The .reduce() Method

PHOTO EMBED

Mon Jul 18 2022 21:09:28 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

//The .reduce() method returns a single value after iterating through the elements of an array, thereby reducing the array. Take a look at the example below:



const newNumbers = [1, 3, 5, 7];

const newSum = newNumbers.reduce((accumulator, currentValue) => {
  console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
  return accumulator + currentValue;

},10)
 
console.log(newSum)
content_copyCOPY

https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/reduce