Skip to content

Instantly share code, notes, and snippets.

@suejungshin
Created November 21, 2019 17:59
Show Gist options
  • Save suejungshin/ba41e34b8d084f42327f89343597e27a to your computer and use it in GitHub Desktop.
Save suejungshin/ba41e34b8d084f42327f89343597e27a to your computer and use it in GitHub Desktop.
Week4-Interview-Problem
class CashAmount {
constructor(value) {
this.inputDouble = value;
this.inputPennies = this.turnDoubleIntoPennies(this.inputDouble);
this.currentTotalInPennies = this.inputPennies;
this.turnDoubleIntoPennies = this.turnDoubleIntoPennies.bind(this);
this.totalInPennies = this.totalInPennies.bind(this);
this.addDoubleAmount = this.addDoubleAmount.bind(this);
this.quantityOfEachDenomination = this.quantityOfEachDenomination.bind(this);
this.toDouble = this.toDouble.bind(this);
this.toDoubleString = this.toDoubleString.bind(this);
}
turnDoubleIntoPennies(double) {
return double * 100;
}
totalInPennies() {
return this.currentTotalInPennies;
}
addDoubleAmount(doubleToAdd) {
this.currentTotalInPennies += this.turnDoubleIntoPennies(doubleToAdd);
}
quantityOfEachDenomination() { // I misunderstood at first what this was supposed to be returning so did it wrong and didn't finish fixing it
let resultObj = {};
let startingNum = this.inputPennies;
let intermediateVal = startingNum;
let denomsToPenniesMap = {
'hundreds': 1 * 100 * 100,
'fifties': 1 * 100 * 50,
'twenties': 1 * 100 * 20,
'tens': 1 * 100 * 10,
'fives': 1 * 100 * 5,
'ones': 1 * 100,
'quarters': 1 * 25,
'dimes': 1 * 10,
'nickels': 1 * 5,
'pennies': 1
}
let denomNamesArr = Object.keys(denomsToPennies)
for (let i = 0; i < denomNamesArr.length; i++) {
resultObj[denomNamesArr[i]] = Math.floor(intermediateVal / denomsToPenniesMap[denomNamesArr[i]]);
intermediateVal = startingNum % denomsToPenniesMap[denomNamesArr[i]];
}
for (let key in denomsToPenniesMap) {
resultObj[key] = Math.ceil(this.inputPennies / denomsToPenniesMap[key]);
}
return resultObj;
}
toDouble() {
return this.currentTotalInPennies / 100;
}
toDoubleString() {
return (this.currentTotalInPennies / 100).toString();
}
}
// Test Suite // Note my 'denominations' function is not working
const cash = new CashAmount(10.50);
console.log(cash.totalInPennies() === 1050) // Should return TRUE
cash.addDoubleAmount(29.33);
console.log(cash.totalInPennies() === 3983) // Should return TRUE
console.log(cash.toDouble() === 39.83) // Should return TRUE
console.log(cash.toDoubleString() === '39.83') // Should return TRUE
const cash2 = new CashAmount(0.10);
cash2.addDoubleAmount(0.20);
console.log(cash2.totalInPennies() === 30) // Should return TRUE
// // Testing
// const cash = new CashAmount(10.50)
// console.log(cash)
// console.log(cash.totalInPennies()) // 1050
// console.log(cash.addDoubleAmount(29.33));
// console.log(cash.totalInPennies()); // 3983
// const cash3 = new CashAmount(967.93);
// console.log(cash3.quantityOfEachDenomination()) // ->
// // {
// // 'hundreds': 9,
// // 'fifties': 1,
// // 'twenties': 0,
// // 'tens': 1,
// // 'fives': 1,
// // 'ones': 2,
// // 'quarters': 3,
// // 'dimes': 1,
// // 'nickels': 1,
// // 'pennies': 3
// // }
// const cash4 = new CashAmount(10.50);
// cash4.addDoubleAmount(29.33);
// console.log(cash4.toDouble()); // -> 39.83
// const cash5 = new CashAmount(10.50);
// cash5.addDoubleAmount(29.33);
// console.log(cash5.toDoubleString()); // -> '39.83'
// const cash6 = new CashAmount(0.10);
// cash6.addDoubleAmount(0.20);
// console.log(cash6.totalInPennies() === 30) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment