Skip to content

Instantly share code, notes, and snippets.

@singhkumarhemant
Last active June 4, 2019 06:32
Show Gist options
  • Save singhkumarhemant/8d0482b48f7464e924caba24268fe0f8 to your computer and use it in GitHub Desktop.
Save singhkumarhemant/8d0482b48f7464e924caba24268fe0f8 to your computer and use it in GitHub Desktop.
const SPENDING_THRESHOLD = 200;
const TAX_RATE = 0.08;
const PHONE_PRICE = 99.99;
const ACCESSORY_PRICE = 9.99;
var bank_balance = 303.91;
var amount = 0;
function calculateTax(amount) {
return amount * TAX_RATE;
}
function formatAmount(amount) {
return "INR" + amount.toFixed( 2 );
}
// keep buying phones while you still have money
while (amount < bank_balance) {
// buy a new phone!
amount = amount + PHONE_PRICE;
if (amount < SPENDING_THRESHOLD) {
amount = amount + ACCESSORY_PRICE;
}
}
amount = amount + calculateTax( amount );
console.log(
"Your purchase: " + formatAmount( amount )
);
if (amount > bank_balance) {
console.log(
"You can't afford this purchase. :("
);
}
@singhkumarhemant
Copy link
Author

singhkumarhemant commented Jun 4, 2019

Write a program to calculate the total price of your phone purchase.
You will keep purchasing phones until you
run out of money in your bank account. You’ll also buy accessories
for each phone as long as your purchase amount is below
your mental spending threshold.

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