1
var maximumBags = function (capacity, rocks, additionalRocks) {5
if (capacity[i] - rocks[i] > 0) {6
// check if rocks[i] is less than capacity[i]7
differences.push(capacity[i] - rocks[i]); // add the difference9
count++; // rocks[i] is at full capacity, so the result goes up by 112
differences.sort((a, b) => a - b); // sort the differences13
var r = additionalRocks;14
while (r != 0 && differences.length > 0) {15
// loop through differences until there is no more rocks or all of the bags of rocks are at full capacity16
if (differences[0] <= r) {17
r -= differences[0]; // minus the difference of the rocks18
count++; // add to result because another bag of rocks is at full capacity20
break; // rocks can not make any other bags at full capacity, so stop right here22
differences.shift(); // remove the first difference