Advertisement
dhows

Untitled

Mar 25th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. let candies = [16, 39, 67, 16, 38, 71]
  2. let threshold = 17;
  3.  
  4. console.log(sugarHigh(candies, threshold))
  5.  
  6. function sugarHigh(candies, threshold){
  7. let candiesSort
  8. var dict = []
  9. for(let i =0; i < candies.length; i++){
  10. dict.push({
  11. key: i,
  12. value: candies[i]
  13. })
  14. }
  15. candiesSort = candies.slice()
  16. candiesSort.sort()
  17. let indices = []
  18. let sumOfSugar = 0
  19. for(let i = 0; i < candiesSort.length; i++){
  20. if(sumOfSugar + candiesSort[i] <= threshold){
  21. sumOfSugar += candiesSort[i]
  22. indices.push(Object.keys(dict).find(key => dict[key].value === candiesSort[i]))
  23. }
  24. }
  25.  
  26. return indices
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement