Maximum Ice Cream Bars LeetCode solution in JavaScript
1var maxIceCream = function (costs, coins) {2costs.sort((a, b) => a - b);3let count = 0;45for (let i = 0; i < costs.length; i++) {6if (costs[i] <= coins) {7count++;8coins -= costs[i];9} else {10break; // a small optimization, end the loop early if coins go down to zero before we reach end of the length of costs.11}12}13return count;14};