784 B
784 B
title, tags, expertise, author, cover, firstSeen, lastUpdated
| title | tags | expertise | author | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|---|
| Partial sum array | math,array | intermediate | chalarangelo | blog_images/river-house-lights.jpg | 2020-05-04T12:20:46+03:00 | 2022-01-30T13:10:13+02:00 |
Creates an array of partial sums.
- Use
Array.prototype.reduce(), initialized with an empty array accumulator to iterate overnums. - Use
Array.prototype.slice()to get the previous partial sum or0and add the current element to it. - Use the spread operator (
...) to add the new partial sum to the accumulator array containing the previous sums.
const accumulate = (...nums) =>
nums.reduce((acc, n) => [...acc, n + (acc.slice(-1)[0] || 0)], []);
accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]