Created
August 1, 2017 12:59
-
-
Save zachfedor/dc7f5101a031b2b87c42f0ddb9e00eac to your computer and use it in GitHub Desktop.
A tiny module to calculate how many Walters you can afford.
This file contains 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; | |
const TARE_PRICE = 0.08; | |
function round(f) { | |
return Math.round(f*Math.pow(10,2))/Math.pow(10,2); | |
} | |
function getNumWalters(money) { | |
var ret = [0, 0]; | |
ret[0]=Math.floor(money/WALTER_PRICE); | |
ret[1]=round(money-ret[0]*WALTER_PRICE); | |
return ret; | |
} | |
/** | |
* Main function | |
* | |
* @param money number | |
* @returns array [total walters, money left] | |
*/ | |
function init(money) { | |
// TODO: validation | |
// - float? | |
// - abs? | |
// - significant figures | |
return new Promise((resolve) => { | |
let totalWalters = 0; | |
while (money >= WALTER_PRICE) { | |
var walters = getNumWalters(money); | |
totalWalters += walters[0]; | |
money = round(walters[1] + TARE_PRICE * walters[0]); | |
} | |
return resolve([totalWalters, money]); | |
}); | |
} | |
// console.log('1', init(1)); | |
// console.log('2', init(2)); | |
init(77).then((value) => { | |
console.log('77', value); | |
}); | |
module.exports = init; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment