Skip to content

Instantly share code, notes, and snippets.

@kjkta
Last active February 3, 2016 07:55
Show Gist options
  • Save kjkta/d0b00833cf07198afafc to your computer and use it in GitHub Desktop.
Save kjkta/d0b00833cf07198afafc to your computer and use it in GitHub Desktop.
'use strict'
let cash = 1000
let tax = 0.01
let basket1 = {
'Phone': 130,
'Accessory': 30,
}
let amount = 0
function findMinValue(obj) {
let min = Infinity
for (let i in obj) {
if (obj[i] < min) min = obj[i]
}
return min
}
function calcTax(amt) {
return amt = amt + (amt * tax)
}
function buyAsMuchAsPossible(basket) {
while (cash > calcTax(findMinValue(basket)) ) {
for (let i in basket) {
if (cash > amount + calcTax(basket[i])) {
amount = amount + basket[i]
console.log(`Scanned item [${i}: $${basket[i]}]`);
console.log(`Current Total $${amount.toFixed(2)}`);
} else console.log(`You don't have $${basket[i]} for a ${i} :(`);
}
amount = calcTax(amount)
cash = cash - amount
}
if (amount === 0) console.log(`You can't afford anything!`);
console.log(`Final Total (inc. Tax) $${amount.toFixed(2)}`);
console.log(`Cash remaining $${cash.toFixed(2)}`);
}
buyAsMuchAsPossible(basket1)
@choonkending
Copy link

Generally I get a little bit nervous when I see for in like this line. Not cause it's wrong but because when you iterate through an object with for in it might iterate through it's prototype's properties as well, read more here. That said, there isn't anything wrong with your code because it's just a normal object :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment