Created
January 13, 2016 13:48
-
-
Save LulzAugusto/87e8e983a084fd239cc9 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
'use strict'; | |
const WALTER_PRICE = 0.53; | |
const TARE_PRICE = 0.08; | |
export default function WalterCalculator(money) { | |
const ret = { totalWalters: 0, leftoverMoney: 0 }; | |
console.log('End'); | |
return new Promise(function(resolve) { | |
if (money < WALTER_PRICE) { | |
ret.totalWalters = 0; | |
ret.leftoverMoney = money; | |
} | |
while (money >= WALTER_PRICE) { | |
const {numWalters, leftoverMoney} = getNumWalters(money); | |
ret.totalWalters += numWalters; | |
money = round(leftoverMoney + TARE_PRICE * numWalters); | |
} | |
ret.leftoverMoney = money; | |
resolve(ret); | |
}); | |
} | |
function getNumWalters(money) { | |
const ret = { numWalters: 0, leftoverMoney: 0 }; | |
ret.numWalters = Math.floor(money/WALTER_PRICE); | |
ret.leftoverMoney = round(money-ret.numWalters*WALTER_PRICE); | |
return ret; | |
} | |
function round(f) { | |
return Math.round(f*Math.pow(10,2))/Math.pow(10,2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment