Last active
February 3, 2016 07:55
-
-
Save kjkta/d0b00833cf07198afafc to your computer and use it in GitHub Desktop.
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
'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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 withfor 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 :)