Created
August 9, 2020 03:09
-
-
Save cplpearce/1d8fe5a232a6674bef0dfb2c1c855741 to your computer and use it in GitHub Desktop.
Kata 11 - Change Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calculateChange = function(total, cash) { | |
// c(hange) | |
let c = { | |
twentyDollar : 0, | |
tenDollar : 0, | |
fiveDollar : 0, | |
twoDollar : 0, | |
oneDollar : 0, | |
quarter : 0, | |
dime : 0, | |
nickel : 0, | |
penny : 0 | |
} | |
// c(urrent)O(wed) | |
let cO = cash - total; | |
switch (true) { | |
case (cO > 0) && (Math.floor(cO / 2000) !== 0): | |
c.twentyDollar = Math.floor(cO / 2000); | |
cO = cO - c.twentyDollar * 2000; | |
console.log(cO) | |
case (cO > 0) && (Math.floor(cO / 1000) !== 0): | |
c.tenDollar = Math.floor(cO / 1000); | |
cO = cO - c.tenDollar * 1000; | |
case (cO > 0) && (Math.floor(cO / 500) !== 0): | |
c.fiveDollar = Math.floor(cO / 500); | |
cO = cO - c.fiveDollar * 500; | |
case (cO > 0) && (Math.floor(cO / 200) !== 0): | |
c.twoDollar = Math.floor(cO / 200); | |
cO = cO - c.twoDollar * 200; | |
case (cO > 0) && (Math.floor(cO / 100) !== 0): | |
c.oneDollar = Math.floor(cO / 100); | |
cO = cO - c.oneDollar * 100; | |
case (cO > 0) && (Math.floor(cO / 25) !== 0): | |
c.quarter = Math.floor(cO / 25); | |
cO = cO - c.quarter * 25; | |
case (cO > 0) && (Math.floor(cO / 10) !== 0): | |
c.dime = Math.floor(cO / 10); | |
cO = cO - c.dime * 10; | |
case (cO > 0) && (Math.floor(cO / 5) !== 0): | |
c.nickel = Math.floor(cO / 5); | |
cO = cO - c.nickel * 5; | |
case (cO > 0) && (Math.floor(cO / 5) !== 0): | |
c.nickel = Math.floor(cO / 5); | |
cO = cO - c.nickel * 5; | |
default: | |
c.penny = cO / 1; | |
cO = cO - c.penny * 1; | |
break; | |
} | |
// sanitize c(hange) array | |
for (let val in c) { | |
c[val] === 0 && (delete c[val]); | |
} | |
return c; | |
}; | |
console.log(calculateChange(1787, 2000)); | |
console.log(calculateChange(2623, 4000)); | |
console.log(calculateChange(501, 1000)); | |
/* | |
valid denominations are as follows: | |
Twenty dollars (2000) | |
Ten dollars (1000) | |
Five dollars (500) | |
Two dollars (200) | |
One dollar (100) | |
Quarter (25) | |
Dime (10) | |
Nickel (5) | |
Penny (1) | |
Expected Output | |
{ twoDollar: 1, dime: 1, penny: 3 } | |
{ tenDollar: 1, twoDollar: 1, oneDollar: 1, quarter: 3, penny: 2 } | |
{ twoDollar: 2, quarter: 3, dime: 2, penny: 4 } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment