-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGot change? .js
26 lines (16 loc) · 993 Bytes
/
Got change? .js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Create a function that will take any amount of money and break it down to the smallest number of bills as possible. Only integer amounts will be input, NO floats. This function should output a sequence, where each element in the array represents the amount of a certain bill type. The array will be set up in this manner:
// array[0] ---> represents $1 bills
// array[1] ---> represents $5 bills
// array[2] ---> represents $10 bills
// array[3] ---> represents $20 bills
// array[4] ---> represents $50 bills
// array[5] ---> represents $100 bills
// In the case below, we broke up $365 into 1 $5 bill, 1 $10 bill, 1 $50 bill, and 3 $100 bills:
// 365 => [0,1,1,0,1,3]
// In this next case, we broke $217 into 2 $1 bills, 1 $5 bill, 1 $10 bill, and 2 $100 bills:
// 217 => [2,1,1,0,0,2]
function giveChange(amount, bills=[100, 50, 20, 10, 5, 1]) {
return bills.map(e=> ([e, amount]=
[Math.floor(amount/e), amount%e])[0]
).reverse()
}