Created
December 13, 2018 17:55
-
-
Save zkliu611/81bd02cefe28604ebc4f398008c51ee0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class CashAmount { | |
| constructor(amount){ | |
| this.value = amount; | |
| } | |
| totalInPennies() { | |
| var string = String(this.value); | |
| if (!string.includes('.')){ | |
| string = string + '00'; | |
| } | |
| var pennies = ''; | |
| for (let i = 0; i < string.length; i++){ | |
| if (string[i] !== '.'){ | |
| pennies += string[i]; | |
| } | |
| } | |
| return Number(pennies); | |
| } | |
| addDoubleAmount(val) { | |
| var total = String(this.totalInPennies(val) + this.totalInPennies(this.value)); | |
| total = Number(totla.slice(0,-2) + '.' +totla.slice(-2)); | |
| return total; | |
| } | |
| quantityOfEachDenomination() { | |
| var obj = {}; | |
| var value = totalInPennies(this.amount); | |
| if (value > 10000) { | |
| obj['hundreds'] = Math.floor(value/10000); | |
| value = value % 10000; | |
| } | |
| if (value > 5000) { | |
| obj['fifties'] = 1; | |
| value = value - 5000; | |
| } | |
| if (value > 2000) { | |
| obj['twenties'] = Math.floor(value/2000); | |
| value = value % 2000; | |
| } | |
| if (value > 1000) { | |
| obj['tens'] = 1; | |
| value = value - 1000; | |
| } | |
| if (value > 500) { | |
| obj['fives'] = 1; | |
| value = value - 500; | |
| } | |
| if (value > 100) { | |
| obj['ones'] = Math.floor(value/100);; | |
| value = value % 100; | |
| } | |
| if (value > 25) { | |
| obj['quarter'] = Math.floor(value/25);; | |
| value = value % 25; | |
| } | |
| if (value > 10) { | |
| obj['dimes'] = Math.floor(value/10);; | |
| value = value % 100; | |
| } | |
| if (value > 5) { | |
| obj['nickles'] = 1;; | |
| value = value - 5; | |
| } | |
| obj[pennies] = value; | |
| return obj; | |
| } | |
| toDouble() { | |
| var value = String(totalInPennies(this.value)) | |
| value = Number(totla.slice(0,-2) + '.' +totla.slice(-2)); | |
| return value; | |
| } | |
| toDoubleString() { | |
| var value = String(totalInPennies(this.value)) | |
| value = totla.slice(0,-2) + '.' +totla.slice(-2); | |
| return value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment