old way:
function accumulate(arr) {let result = []let sum = 10for (let i = 0; i < arr.length; i++) {sum += arr[i]result.push(sum)}return result}
in functional programming
- no mutation - don't mutate data
- pure function - no side effects
- const
so we need to use filter, map, concat, reduce,
function accumulate(arr) {
let total = 10
return (() =>
arr.map((num) => {
total += num
return total
})
)
}
console.log(sum([1,2,3])())
=> [ 11, 13, 16 ]