This is an example of what I need to do:
var myarray = [5, 10, 3, 2];
var result1 = myarray[0];
var result2 = myarray[1] + myarray[0];
var result3 = myarray
A simple function using array-reduce.
const arr = [6, 3, -2, 4, -1, 0, -5];
const prefixSum = (arr) => {
let result = [arr[0]]; // The first digit in the array don't change
arr.reduce((accumulator, current) => {
result.push(accumulator + current);
return accumulator + current; // update accumulator
});
return result;
}