Created
August 3, 2017 16:56
-
-
Save tvolk131/03c8e4948f89e50db74557f5308ac0a8 to your computer and use it in GitHub Desktop.
Hack Reactor CashAmount Interview Prep
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
class CashAmount { | |
constructor (amount) { | |
this.pennyCount = 0; | |
this.addDoubleAmount(amount); | |
} | |
totalAsPennies () { | |
return this.pennyCount; | |
} | |
totalInPennies () { | |
return this.totalAsPennies(); | |
} | |
addDoubleAmount (double) { | |
var roundedPennyCount = Math.floor(double * 100); | |
this.pennyCount += roundedPennyCount; | |
} | |
quantityOfEachDenomination () { | |
var denominations = [[10000, 'hundreds'], [5000, 'fifties'], [2000, 'twenties'], [1000, 'tens'], [500, 'fives'], [100, 'ones'], [25, 'quarters'], [10, 'dimes'], [5, 'nickels'], [1, 'pennies']]; | |
var amount = this.pennyCount; // Since we need a modifiable copy of this.pennyCount to work with | |
var denominationAmounts = {}; | |
for (var i = 0; i < denominations.length; i++) { | |
denominationAmounts[denominations[i][1]] = 0; | |
while (amount >= denominations[i][0]) { | |
amount -= denominations[i][0]; | |
denominationAmounts[denominations[i][1]]++; | |
} | |
} | |
return denominationAmounts; | |
} | |
toDouble () { | |
return this.pennyCount / 100; | |
} | |
toDoubleString () { | |
return this.toDouble().toString(); | |
} | |
} | |
class TestSuite { | |
runTests () { | |
this.passed = 0; | |
this.failed = 0; | |
var cash = new CashAmount(10.50); | |
this.expect(cash.totalInPennies(), 1050, 'totalInPennies() returns correct value'); | |
cash.addDoubleAmount(29.33); | |
this.expect(cash.totalAsPennies(), 3983, 'addDoubleAmount() properly mutates CashAmount value'); | |
this.expect(cash.toDouble(), 39.83, 'toDouble() returns correct value'); | |
this.expect(cash.toDoubleString(), '39.83', 'toDoubleString() returns correct value'); | |
cash = new CashAmount(967.93); | |
this.expectObj(cash.quantityOfEachDenomination(), { | |
'hundreds': 9, | |
'fifties': 1, | |
'twenties': 0, | |
'tens': 1, | |
'fives': 1, | |
'ones': 2, | |
'quarters': 3, | |
'dimes': 1, | |
'nickels': 1, | |
'pennies': 3 | |
}, 'quantityOfEachDenomination() returns correct values'); | |
cash = new CashAmount(0.10); | |
cash.addDoubleAmount(0.20); | |
this.expect(cash.totalInPennies(), 30, 'Should retain floating-point precision when adding amounts less than a dollar'); | |
if (this.passed + this.failed > 0) { | |
if (this.failed === 0) { | |
console.log('All ' + this.passed + ' tests passed'); | |
} else { | |
console.log('Passed ' + this.passed + ' tests'); | |
console.log('Failed ' + this.failed + ' tests'); | |
} | |
} else { | |
console.log('No tests were run'); | |
} | |
} | |
expect (val1, val2, testName) { | |
if (val1 === val2) { | |
this.passed++; | |
} else { | |
console.error('Failed: ' + testName); | |
this.failed++; | |
} | |
} | |
expectObj (val1, val2, testName) { | |
if (JSON.stringify(val1) === JSON.stringify(val2)) { | |
this.passed++; | |
} else { | |
console.error('Failed: ' + testName); | |
this.failed++; | |
} | |
} | |
} | |
new TestSuite().runTests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment