how to find how many items need to sum to get given sum in array javascript

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

68 thoughts on “how to find how many items need to sum to get given sum in array javascript”

  1. 294702 404410I like what you guys are up also. Such intelligent work and reporting! Maintain up the superb works guys Ive incorporated you guys to my blogroll. I feel itll improve the value of my website . 373657

    Reply

Leave a Comment