Skip to content

Latest commit

 

History

History
17 lines (13 loc) · 442 Bytes

accumulate.md

File metadata and controls

17 lines (13 loc) · 442 Bytes
title tags
accumulate
math,array,beginner

Returns an array of partial sums.

  • Use Array.prototype.reduce(), Array.prototype.slice(-1) and the unary + operator to add each value to the unary array containing the previous sum.
const accumulate = (...nums) => nums.reduce((acc, n) => [...acc, n + +acc.slice(-1)],[]);
accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]