Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let candies = [16, 39, 67, 16, 38, 71]
- let threshold = 17;
- console.log(sugarHigh(candies, threshold))
- function sugarHigh(candies, threshold){
- let candiesSort
- var dict = []
- for(let i =0; i < candies.length; i++){
- dict.push({
- key: i,
- value: candies[i]
- })
- }
- candiesSort = candies.slice()
- candiesSort.sort()
- let indices = []
- let sumOfSugar = 0
- for(let i = 0; i < candiesSort.length; i++){
- if(sumOfSugar + candiesSort[i] <= threshold){
- sumOfSugar += candiesSort[i]
- indices.push(Object.keys(dict).find(key => dict[key].value === candiesSort[i]))
- }
- }
- return indices
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement