-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeCalculator.js
More file actions
56 lines (55 loc) · 1.64 KB
/
changeCalculator.js
File metadata and controls
56 lines (55 loc) · 1.64 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const calculateChange = function(total, cash) {
if (total > cash){ return false};
let change = cash - total;
let expected = {};
let howManyTimes = 0;
if(change > 2000){
howManyTimes = Math.trunc(change / 2000);
change -= 2000 * howManyTimes;
expected['twentyDollar'] = howManyTimes;
}
if(change > 1000){
howManyTimes = Math.trunc(change / 1000);
change -= 1000 * howManyTimes;
expected['tenDollar'] = howManyTimes;
}
if(change > 500){
howManyTimes = Math.trunc(change / 500);
change -= 500 * howManyTimes;
expected['fiveDollar'] = howManyTimes;
}
if(change > 200){
howManyTimes = Math.trunc(change / 200);
change -= 200 * howManyTimes;
expected['twoDollar'] = howManyTimes;
}
if(change > 100){
howManyTimes = Math.trunc(change / 100);
change -= 100 * howManyTimes;
expected['oneDollar'] = howManyTimes;
}
if(change > 25){
howManyTimes = Math.trunc(change / 25);
change -= 25 * howManyTimes;
expected['quarter'] = howManyTimes;
}
if(change > 10){
howManyTimes = Math.trunc(change / 10);
change -= 10 * howManyTimes;
expected['dime'] = howManyTimes;
}
if(change > 5){
howManyTimes = Math.trunc(change / 5);
change -= 5 * howManyTimes;
expected['nickel'] = howManyTimes;
}
if(change > 1){
howManyTimes = Math.trunc(change / 1);
change -= 1 * howManyTimes;
expected['penny'] = howManyTimes;
}
return expected;
};
console.log(calculateChange(1787, 2000));
console.log(calculateChange(2623, 4000));
console.log(calculateChange(501, 1000));