Write a method that takes an integer and returns the smallest number of bills in US currency that could make up that integer value.
The currency denominations will be: 1, 5, 10, 20, 50, and 100.
So for example, If the input integer is 26, then the output would be 3: a 20, a 5, and a 1. 3 bills, so return the number 3.
If the input is 156, then the output would be 4: a 100, a 50, a 5, and a 1. 4 bills, so return the number 4.
Initially I wrote it using a while loop nested in a for loop, then refactored it to ES6 using map. How could I refactor my answer to be better?
let getNumBills = (amount) => {
if(amount <= 0) return 0;
let billCount = 0;
const billDenominations = [100, 50, 20, 10, 5, 1];
billDenominations.map(e => {
while(amount >= e) {
billCount++;
amount -= e;
if(amount == 0) return(billCount);
}
});
return(billCount);
}