Files
30-seconds-of-code/snippets/accumulate.md
Angelos Chalaris b3e2b52991 Add accumulate
2020-05-04 12:20:46 +03:00

18 lines
440 B
Markdown

---
title: accumulate
tags: 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.
```js
const accumulate = (...nums) => nums.reduce((acc, n) => [...acc, n + +acc.slice(-1)],[]);
```
```js
accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]
```