reduce를 사용하는 방식은 아주 큰 값을 만났을 때 오류가 발생할 확률을 감소시키고, 앞선 방식보다 메모리 효율도 좋음
let a = [6, 5, 4, 3, 2];
함수 만들기
const reducerOne = (accumulator, currentvalue) => accumulator + currentvalue;
const reducerTwo = (accumulator, currentvalue) => accumulator - currentvalue;
최댓값
const reducerThree = (accumulator, currentvalue)
=> accumulator > currentvalue ? accumulator : currentvalue;
console.log(a.reduce(reducerThree)); // 6
최솟값
const reducerThree = (accumulator, currentvalue)
=> accumulator < currentvalue ? accumulator : currentvalue;
console.log(a.reduce(reducerThree)); // 1
✨참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce