Skip to content

Instantly share code, notes, and snippets.

@OneCent01
Created June 15, 2017 16:58
Show Gist options
  • Save OneCent01/eb1596ad48535dbe40a30ed357714c52 to your computer and use it in GitHub Desktop.
Save OneCent01/eb1596ad48535dbe40a30ed357714c52 to your computer and use it in GitHub Desktop.
class CashAmount {
constructor(amount) {
this.amount = amount;
}
}
CashAmount.prototype.totalInPennies = function() {
var pennies = this.amount * 100;
return pennies;
};
CashAmount.prototype.addDoubleAmount = function(doubleAmount) {
this.amount = this.amount * 100;
this.amount += (doubleAmount * 100);
this.amount /= 100;
return this.amount;
};
CashAmount.prototype.quantityOfEachDenomination = function() {
var denominations = [
'thousands',
'hundreds',
'fifties',
'twenties',
'tens',
'fives',
'ones',
'quarters',
'dimes',
'nickels',
'pennies'
];
var amountArray = (this.amount * 100 + '').split('');
var storage = {};
while(amountArray.length > 0) {
var popNumber = amountArray.pop();
var popString = denominations.pop();
storage[popString] = popNumber;
}
return storage;
};
CashAmount.prototype.toDouble = function() {
return this.amount;
};
CashAmount.prototype.toDoubleString = function() {
return this.amount.toFixed(2);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment