Created
February 11, 2017 17:35
-
-
Save zwhitchcox/5ea638c71c2836fa3be920ce764e7fe9 to your computer and use it in GitHub Desktop.
generates list of gas prices with the number of gallons it would take to have an price and number of gallons with an integer
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
| // price = price * 100 for simplicity | |
| var str = '' | |
| for (let price = 1; price <= 400; price++) { | |
| let gallons = 1 | |
| while (gallons*price % 100 !== 0) | |
| gallons++ | |
| str+=format(price, gallons, gallons*price) | |
| } | |
| function format(gasPrice, totalGallons, totalPrice) { | |
| return padRight(`Gas Price: ${currency(gasPrice)}`, ' ', 22) + | |
| padRight(`Total Gallons: ${padLeft(totalGallons, ' ', 3)}.00`, ' ', 22) + | |
| `Total Price: ${padLeft(currency(totalPrice), ' ', 7)}`+'\n' | |
| } | |
| console.log(str) | |
| function currency(number) { | |
| return `$${number/100|0}.${padLeft(number%100, 0, 2)}` | |
| } | |
| function padRight(start, padding, padLength) { | |
| let str = ''+start | |
| while (str.length < padLength) | |
| str = str + padding | |
| return str | |
| } | |
| function padLeft(start, padding, padLength) { | |
| let str = ''+start | |
| while (str.length < padLength) | |
| str = padding + str | |
| return str | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment