Find max number of items that can fit in a given max-width
I find your code clear enough, but you could improve it a little bit, so you only do the add operation once:

function getNumberOfElementsInWidth(elements, maxWidth) {
  let accWidth = 0;
  let count = 0;
  for (let i = 0; i &lt; elements.length; i++) {
    accWidth += elements[i].width
    if (accWidth &lt;= maxWidth) {
      count++;
    } else {
      break;
    }
  }
  return count;
}




Further you could reverse the conditional statement:

function getNumberOfElementsInWidth(elements, maxWidth) {
  let accWidth = 0;
  let count = 0;
  for (let i = 0; i &lt; elements.length; i++) {
    accWidth += elements[i].width
    if (accWidth &gt; maxWidth) {
      break;
    }

    count++;
  }
  return count;
}




In a more condensed version you could make the count variable also be the index:

function getNumberOfElementsInWidth(elements, maxWidth) {
  let accWidth = 0;

  for (var count = 0; count &lt; elements.length; count++) {
    if ((accWidth += elements[count].width) &gt; maxWidth) {
      break;
    } 
  }
  return count;
}


Be aware that count must be declared as var count = 0 not let count = 0