JSFiddle - React, Tailwind, and code Playground

by apasaja

JavaScript

const prices = [1,2,3,4,5];
const k = 12;

/* console.log('111', prices.sort(function(a,b) {
  return a - b
  })
) */

/* let spent = 0;
let count = 0;
for(let toy of prices){
  spent += toy
  console.log('aaa', spent)
  if(spent >=k) {
    return count;
  }
  count++
} */


const test = function maximumToys(prices, k) {
  let spent = 0;
  let count = 0;
  console.log('1>>>', prices)
  console.log('2>>>', k)
  
  prices.sort(function(a,b){
    return a - b;
  });
  
  for(let toy of prices){
    spent += toy;
    if(spent >= k){
      return count;
    }
    count++;    
  }
}

maximumToys(5, 7)