# Cumulative Sum Array in Javascript - One is working and one is not

Hi, I’ve been attempting some katas on codewars and have got stuck with this one. I wonder if anyone can help me out and explain what is happening with this code. At this point I’m not looking for a solution for the whole exercise, I’d just like to understand why the const cumulativeSum is working differently for the two different arrays.

I’m creating two cumulative arrays, the code works for the first one ( cumulativeProfit with the starting array peopleInLine ) and everything comes out correctly but when I use it for the second one ( cumulativeOutgoings with the starting array changeRequired ) the figures are wrong.

My peopleInLine array is: [100, 25, 50, 100, 25, 25, 25, 100, 25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100]

I have to admit that I don’t really understand how const cumulativeSum = (sum => value => sum += value)(0) works. I found it after searching on stack overflow!

Very grateful for any assistance.

``````

let changeRequired = [];

const cumulativeSum = (sum => value => sum += value)(0);

let cumulativeProfit = peopleInLine.map(cumulativeSum);
cumulativeProfit.splice(0, 0, 0);
cumulativeProfit.pop();
//return cumulativeProfit;
//correctly logs to console: [0, 100, 125, 175, 275, 300, 325, 350, 450, 475, 525, 550, 650, 675, 700, 750, 850, 875, 900, 950]

for (let i = 0; i < peopleInLine.length; i++) {
if (peopleInLine[i] === 25) { changeRequired.push(0) }
else if (peopleInLine[i] === 50) { changeRequired.push(25) }
else if (peopleInLine[i] === 100) { changeRequired.push(75) }
};
//return changeRequired;
//correctly logs to console: [75, 0, 25, 75, 0, 0, 0, 75, 0, 25, 0, 75, 0, 0, 25, 75, 0, 0, 25, 75]

let cumulativeOutgoings = changeRequired.map(cumulativeSum);
cumulativeOutgoings.splice(0, 0, 0);
cumulativeOutgoings.pop();
return cumulativeOutgoings;
//incorrectly logs to console: [0, 1125, 1125, 1150, 1225, 1225, 1225, 1225, 1300, 1300, 1325, 1325, 1400, 1400, 1400, 1425, 1500, 1500, 1500, 1525]
}
console.log(tickets([100, 25, 50, 100, 25, 25, 25, 100, 25, 50, 25, 100, 25, 25, 50, 100, 25, 25, 50, 100]));
``````

Hi Nicola,

the code snippet is messed up, could you provide the full code, please?
Can you go more into detail as to why you think the figures are wrong and what numbers you expect?

It’s a closure and it is already called with the argument 0.
You could also write it like this:

``````const cumulativeSum = sum => {
return value => sum += value
}
const cumSum = cumulativeSum(0);
let cumulativeProfit = peopleInLine.map(cumSum);

console.log(cumulativeProfit);
//[100, 125, 175, 275, 300, 325, 350, 450, 475, 525, 550, 650, 675, 700, 750, 850, 875, 900, 950, 1050]
``````
1 Like

Hi Thanks for your answer, changing adding a zero worked. It should be cumulatively summing the array ‘changeRequired’ but the second value is over a thousand etc. so they are completely wrong.

As it happens I was going about this exercise in completely the wrong way with the wrong logic so I had to start again anyway and once I understood what I had to do it was pretty quick. Oops I’d spent ages on it

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.