Q:

Write a JavaScript program to apply a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values

0

 Write a JavaScript program to apply a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const reduceSuccessive = (arr, fn, acc) =>
  arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);

console.log(reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now