var arr = [2, 5, 9, 11, 1, 2, 4], x = 12, sum = 0; arr.length = arr.sort((a, b)=> b-a).findIndex((el) => (sum += el) > x )+1; if(!arr.length) arr = false; alert(arr);