I have this JS to get the widths of a bunch of sibling elements and add those widths to an array.
var nav = document.querySelector('.js-primary-nav');
var childrenWidths = [];
for(var i=0; i<nav.childElementCount; i++){
childrenWidths.push(nav.children[i].offsetWidth);
}
Now I want to cycle through these widths, adding them up until the total is greater than X (which will be the container width). I then want to get the positions of the values up until the one which tipped the total over into larger than X so I can add those values to one pile and the others to another.
How do I cycle through the values, adding and then comparing the total to a number?
Thanks