Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
Created February 11, 2017 17:35
Show Gist options
  • Select an option

  • Save zwhitchcox/5ea638c71c2836fa3be920ce764e7fe9 to your computer and use it in GitHub Desktop.

Select an option

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
// 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