Skip to content

Instantly share code, notes, and snippets.

@twalk4821
Created March 9, 2017 17:57
Show Gist options
  • Save twalk4821/0d2292e94120a345002df36ab7034b6e to your computer and use it in GitHub Desktop.
Save twalk4821/0d2292e94120a345002df36ab7034b6e to your computer and use it in GitHub Desktop.
class CashAmount {
constructor(double) {
this.amount = double;
}
totalAsPennies() {
return Math.floor(this.amount*100);
}
addDoubleAmount(double) {
this.amount = Math.floor(this.amount + double);
}
quantityOfEachDenomination() {
const denominations = ['hundreds', 'fifties', 'twenties', 'tens', 'fives', 'ones', 'quarters', 'dimes', 'nickels', 'pennies'];
const quantities = [100, 50, 20, 10, 5, 1, .25, .1, .5, .01];
let wallet = {};
let remaining = this.amount;
for (let i = 0; i<10; i++) {
wallet[denominations[i]] = 0;
while (remaining - quantities[i] > 0) {
remaining -= quantities[i];
wallet[denominations[i]] += 1;
}
}
return wallet;
}
toDouble() {
return this.amount;
}
toDoubleString() {
return this.amount.toString();
}
}
class TestSuite {
testTotalInPennies() {
const cash = new CashAmount(10.50);
if (cash.totalInPennies() !== 1050) {
console.log('Fail in totalInPennies');
return false;
}
}
testAddDoubleAmount() {
const cash = new CashAmount(10.50);
cash.addDoubleAmount(29.33);
if (cash.totalAsPennies() !== 3983) {
console.log('Fail in add double');
return false;
}
}
testQuanitityOfEachDenomination() {
const cash = new CashAmount(967.93);
const obj =
{
'hundreds': 9,
'fifties': 1,
'twenties': 0,
'tens': 1,
'fives': 1,
'ones': 2,
'quarters': 3,
'dimes': 1,
'nickels': 1,
'pennies': 3
};
const wallet = cash.quantityOfEachDenomination();
for (let denom in wallet) {
if (wallet[denom] !== obj[denom]) {
console.log('fail in denominaiton, denom = ', denom);
return false;
}
}
}
testToDouble() {
const cash = new CashAmount(10.50);
cash.addDoubleAmount(29.33);
if (cash.toDouble() !== 39.83) {
console.log('fail in to double');
return false;
}
}
testToDoubleString() {
const cash = new CashAmount(10.50);
cash.addDoubleAmount(29.33);
if (cash.toDoubleString() !== '39.83') {
console.log('fail ni to string');
return false;
}
}
test() {
this.testTotalInPennies();
this.testAddDoubleAmount();
this.testQuanitityOfEachDenomination();
this.testToDouble();
this.testToDoubleString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment