Skip to content

Instantly share code, notes, and snippets.

@fcgomes92
Last active September 27, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcgomes92/bf1f258078de3b1ff5b2fc18cf2a0784 to your computer and use it in GitHub Desktop.
Save fcgomes92/bf1f258078de3b1ff5b2fc18cf2a0784 to your computer and use it in GitHub Desktop.
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