Aternative way of choosing largest numbers:
var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);
function getHighNums(arr,percentage,fillpercentage){
var perc=0;
var sorted=arr.sort(function(a,b){ return a-b});
var total=0;
for(var i=0;i<arr.length;i++) total+=arr[i];
for(var j=sorted.length-1;j>=0;j--){
perc+=sorted[j]/total*100;
if(fillpercentage){
if(perc > percentage) return sorted.slice(j,sorted.length);
}else{
if(sorted[j]/total*100 < percentage) return sorted.slice(j+1,sorted.length);
}
}
return sorted;
}
console.log(getHighNums(set, 10, false))//9,12,15
console.log(getHighNums(set, 50, true))//12,15
The first line gets all numbers that are at least 10% of sum of array value. Sum of array= 1+2+1+2+3+9+12+15=45
so it picks numbers > 4.5
The second line gets numbers until their sum is at least 50% of the total sum of the array. So as sum of array =45
, it will pick highest numbers until their sum is > 22.5