From a2dc83e9a963c75477dd040e22720e2e14e0ec3b Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 18 Oct 2020 20:00:18 +0300 Subject: [PATCH] Update accumulate --- snippets/accumulate.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/snippets/accumulate.md b/snippets/accumulate.md index db902ed76..84a0d4ef6 100644 --- a/snippets/accumulate.md +++ b/snippets/accumulate.md @@ -1,11 +1,12 @@ --- title: accumulate -tags: math,array,beginner +tags: math,array,intermediate --- -Returns an array of partial sums. +Creates 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. +- Use `Array.prototype.reduce()`, initialized with an empty array accumulator to iterate over `nums`. +- Use `Array.prototype.slice(-1)`, the spread operator (`...`) and the unary `+` operator to add each value to the accumulator array containing the previous sums. ```js const accumulate = (...nums) => nums.reduce((acc, n) => [...acc, n + +acc.slice(-1)],[]);