Skip to content

Instantly share code, notes, and snippets.

@samwhaleIV
Created June 9, 2019 03:47
Show Gist options
  • Save samwhaleIV/a417c3063bcf7ce5cf1dd72c43c22731 to your computer and use it in GitHub Desktop.
Save samwhaleIV/a417c3063bcf7ce5cf1dd72c43c22731 to your computer and use it in GitHub Desktop.
const money = function() {
const cent = "¢";
const addCents = (base,amount) => {
for(let i = 0;i<amount;i++) {
base += cent;
}
return base;
}
let cents = "";
let debt = "";
this.getAmount = () => {
return cents.length - debt.length;
}
this.withdrawl = (amount=0) => {
if(amount < 0) {
this.deposit(-amount);
return;
}
const difference = cents.length - amount;
if(difference < 0) {
debt = addCents(debt,Math.abs(difference));
}
cents = cents.substring(0, difference);
}
this.deposit = (amount=0) => {
if(amount < 0) {
this.withdrawl(-amount);
return;
}
const gain = amount - debt.length;
debt = debt.substring(0,debt.length-amount);
if(gain) {
cents = addCents(cents,gain);
}
}
}
const balance = new money();
balance.deposit(9);
balance.withdrawl(11);
balance.deposit(0);
balance.withdrawl(0);
balance.deposit(10);
balance.withdrawl(100);
console.log(balance.getAmount());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment