Last active
September 27, 2017 14:59
-
-
Save smokinggoats/bf1f258078de3b1ff5b2fc18cf2a0784 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 WALTER_PRICE = 0.53, | |
TARE_PRICE = 0.08; | |
const tareMoney = (bottles) => { | |
return TARE_PRICE * bottles; | |
}; | |
const round = (f) => { | |
return Math.round(f * Math.pow(10, 2)) / Math.pow(10, 2); | |
}; | |
const getNumWalters = async(money) => { | |
var ret = [0, 0]; | |
// calculates the amount of beer | |
ret[0] = Math.floor(money / WALTER_PRICE); | |
// calculates the money left ( including the tare money ) | |
ret[1] = round(money - ret[0] * WALTER_PRICE + tareMoney(ret[0])); | |
return ret; | |
}; | |
const calculate = async(initialMoney) => { | |
// gets the amount of beer based on the initial money | |
let nextIteration = await getNumWalters(initialMoney); | |
let totalWalters = nextIteration[0]; | |
// while there is money left we should get more beers | |
while (nextIteration[1] > WALTER_PRICE) { | |
nextIteration = await getNumWalters(nextIteration[1]); | |
// adds to the total of beers | |
totalWalters += nextIteration[0]; | |
} | |
return {totalWalters: totalWalters, moneyLeft: nextIteration[1]}; | |
} | |
module.exports = calculate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment