I have array
var array = [4,2,6,8,1,6,5,2];
var samNeeded = 20;
as above i need to get how many items need to sum from start of the array to get sum needed
example needed output
var output = [4,2,6,8]
as first four items need to sum up to get the total 20
Here is my solution:
Not sure this is the best approach, but it works for your example:
You could use
Array#reduce
.