I'm trying to remove the last items of an array as long as the sum of the array is higher than a limit. The code I have works but seems to cause slow downs or crash. I was wondering if there's a more elegant solution.
let array = [166, 157, 251, 171, 191];
let limit = 400;
for (
let sum = array.reduce((a, b) => a + b); sum > limit; sum = array.reduce((a, b) => a + b)
) {
array.pop();
}
console.log(array);