Your code checks the smaller bills before it checks the larger bills, which is clearly wrong: you want to have as many $1000 bills as possible before you resort to using $1 bills. So, you can reverse your bills array, or just sort it so it’s reversed:
function change(amount) {
const bills = [1000, 500, 200, 100, 50, 20, 10, 5, 2, 1];
// alternatively, to sort it
// bills.sort((a, b) => b - a);
const result = [];
for (const bill of bills) {
const billCount = Math.floor(amount / bill);
amount = amount % bill;
result.push(...new Array(billCount).fill(bill));
}
return result;
}
solved How do I make this change method, so I get the bills for that given amount [duplicate]