Skip to content

Instantly share code, notes, and snippets.

@mikeliao97
Last active March 9, 2017 17:50
Show Gist options
  • Save mikeliao97/a1dab5593155311226423de29a53c6f6 to your computer and use it in GitHub Desktop.
Save mikeliao97/a1dab5593155311226423de29a53c6f6 to your computer and use it in GitHub Desktop.
class CashAmount {
constructor(amount) {
this.amount = amount;
this.COINS = [ ['hundreds', 100], ['fifties', 50],['twenties', 20], ['tens', 10],
['fives', 5],
['ones', 1],
['quarters', 0.25],
['dimes', 0.10],
['nickels', 0.05],
['pennies', 0.01]
]
}
totalInPennies() {
return this.amount / 0.01;
}
addDoubleAmount(amount) {
this.amount += amount;
}
quantityOfEachDenomination() {
var answer = {};
var tempAmount = this.amount;
for (var a = 0; a < this.COINS.length; a++) {
var currentCoin = this.COINS[a];
var divisor = Math.floor(tempAmount / currentCoin[1]);
answer[currentCoin[0]] = divisor;
tempAmount = tempAmount - (divisor * currentCoin[1]);
tempAmount = tempAmount.toFixed(2);
}
return answer;
}
toDouble() {
return this.amount.toFixed(2);
}
toDoubleString() {
return this.amount.toFixed(2).toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment