Created
June 9, 2019 03:47
-
-
Save samwhaleIV/a417c3063bcf7ce5cf1dd72c43c22731 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
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