Skip to content

Instantly share code, notes, and snippets.

@junjunparkpark
Created June 15, 2017 17:04
Show Gist options
  • Save junjunparkpark/733d44eb1cb22e16e6bc05d1af5f5e9d to your computer and use it in GitHub Desktop.
Save junjunparkpark/733d44eb1cb22e16e6bc05d1af5f5e9d to your computer and use it in GitHub Desktop.
Class Amount Class Interview Prompt
class CashAmount {
constructor(amount) {
this.amount = JSON.stringify(amount);
}
totalInPennies() {
var total = '';
this.amount.toString().split('').forEach(digit => { if (digit !== '.') { total += digit;} })
return total;
}
addDoubleAmount(amount) {
var total = [];
var first = this.amount.split('.');
var second = new CashAmount(amount).amount.split('.');
for (let i = 0; i < first.length; i++) {
var sum = (parseInt(first[i], 10) + parseInt(second[i], 10));
if (i === 1) { total.push('.'); }
if (i === 1 && sum >= 100) {
total[0] += 1;
sum -= 100;
}
sum = JSON.stringify(sum)
if (i === 1 && sum < 10) { total.push('0') }
total.push(sum);
}
return total.join('')
}
quantityOfEachDonomination(amount) {
var dollars = parseInt(amount.toString().split('.')[0]);
var cents = parseInt(amount.toString().split('.')[1]);
var denominations = {
'hundreds': 0,
'fifties': 0,
'twenties': 0,
'tens': 0,
'fives': 0,
'ones': 0,
'quarters': 0,
'dimes': 0,
'nickels': 0,
'pennies': 0
}
while (dollars > 0) {
if (dollars >= 100) {
dollars -= 100;
denominations.hundreds ++;
} else if (dollars >= 50) {
dollars -= 50;
denominations.fifties ++;
} else if (dollars >= 20) {
dollars -= 20;
denominations.twenties ++;
} else if (dollars >= 10) {
dollars -= 10;
denominations.tens++;
} else if (dollars >= 5) {
dollars -= 5;
denominations.fives++;
} else if (dollars >= 1) {
dollars -= 1;
denominations.ones++;
}
}
return denominations
}
toDouble() {
}
toDoubleString() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment