Skip to content
Advertisement

To group number of data upto a certain limit

If anyone could help me out to solve this problem, that would be greatly appreciated. Here I have a list of weights (kg) of a few samples.

JavaScript

The maximum weight that could be for any sample is 1 kg. I need to sum sample weights until the total mass is not exceeding 1 kg. For instance, this is what I expect after summing the weights of samples based on the given condition. 

JavaScript

this is what I have so far, but it loops over infinitely.

JavaScript

I am open to entirely new methods as well, if there any module or easy way to group this, please kindly assist me.

Advertisement

Answer

This works, you are currently looping for the same weight everytime, instead, you should iterate the list like this:

JavaScript

gives

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement