-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange.js
38 lines (32 loc) · 1.31 KB
/
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
27
28
29
30
31
32
33
34
35
36
37
38
// A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1. Your task is to
// write a program that will be used in a vending machine to return change. Assume that the vending
// machine will always want to return the least number of coins or notes. Devise a function
// getChange(M, P) where M is how much money was inserted into the machine and P the price of the
// item selected, that returns an array of integers representing the number of each denomination to
// return.
// Example: getChange(5, 0.99) should return [1,0,0,0,0,4]
/**
* @param M
* @param P
* @returns {*}
*/
function getChange(M, P) {
const denominations = [1, 5, 10, 25, 50, 100];
const result = [0, 0, 0, 0, 0, 0];
let desiredChange = M * 100 - P * 100;
let change = 0;
let currentDenomination = denominations.length - 1;
while (change !== desiredChange && currentDenomination >= 0) {
if (change + denominations[currentDenomination] <= desiredChange) {
change += denominations[currentDenomination];
result[currentDenomination]++;
} else {
currentDenomination--;
}
}
return result;
}
console.log(5, 0.99, getChange(5, 0.99));
console.log(getChange(3.14, 1.99), [0, 1, 1, 0, 0, 1]);
console.log(getChange(4, 3.14), [1, 0, 1, 1, 1, 0]);
console.log(getChange(0.45, 0.34), [1, 0, 1, 0, 0, 0]);