Skip to content

Instantly share code, notes, and snippets.

@DaniGithub
Last active April 16, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaniGithub/ce25ee01f940e8923c18d8db11ba06d8 to your computer and use it in GitHub Desktop.
Save DaniGithub/ce25ee01f940e8923c18d8db11ba06d8 to your computer and use it in GitHub Desktop.
YDKJS: Up & Going Exercise page 26 https://github.com/getify/You-Dont-Know-JS/blob/master/up%20&%20going/ch1.md # Chapter 1: Into Programming
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 "$" + amount.toFixed( 2 );
}
// keep buying phones while you still have money
while (amount < bank_balance) {
// buy a new phone!
amount = amount + PHONE_PRICE;
// can we afford the accessory?
if (amount < SPENDING_THRESHOLD) {
amount = amount + ACCESSORY_PRICE;
}
}
// don't forget to pay the government, too
amount = amount + calculateTax( amount );
console.log(
"Your purchase: " + formatAmount( amount )
);
// Your purchase: $334.76
// can you actually afford this purchase?
if (amount > bank_balance) {
console.log(
"You can't afford this purchase. :("
);
}
// You can't afford this purchase. :(
/* ===============================================================================
Practice
There is absolutely no substitute for practice in learning programming. No amount of articulate writing on my part is alone going to make you a programmer.
With that in mind, let's try practicing some of the concepts we learned here in this chapter. I'll give the "requirements," and you try it first. Then consult the code listing below to see how I approached it.
Write a program to calculate the total price of your phone purchase. You will keep purchasing phones (hint: loop!) 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.
After you've calculated your purchase amount, add in the tax, then print out the calculated purchase amount, properly formatted.
Finally, check the amount against your bank account balance to see if you can afford it or not.
You should set up some constants for the "tax rate," "phone price," "accessory price," and "spending threshold," as well as a variable for your "bank account balance.""
You should define functions for calculating the tax and for formatting the price with a "$" and rounding to two decimal places.
Bonus Challenge: Try to incorporate input into this program, perhaps with the prompt(..) covered in "Input" earlier. You may prompt the user for their bank account balance, for example. Have fun and be creative!
OK, go ahead. Try it. Don't peek at my code listing until you've given it a shot yourself!
===============================================================================*/
function tax(amount) {
return amount * TAX_RATE;
}
function formatAmount(amount, currency) {
return currency + amount.toFixed(2);
}
const TAX_RATE = 0.21;
const PHONE_PRICE = 99.99;
const ACC_PRICE = 9.99;
const ACC_PURCHASE_THRESHOLD = 30;
var bankBalance = 1000;
var quantity = parseInt(prompt("How many phones do you want to buy at a time?", 2));
var total = 0;
while(total < bankBalance) {
total = ACC_PRICE <= ACC_PURCHASE_THRESHOLD ? (PHONE_PRICE + ACC_PRICE) * quantity: PHONE_PRICE * quantity;
total = total + tax(total);
bankBalance -= total;
document.write(`<h2>Your total is ${formatAmount(total, "$")} and your account balance is ${formatAmount(bankBalance, "$")} after the transaction</h2><br/>`);
if(total > bankBalance) document.write(`<h2 style="color:red">You dont have enough money. Your account balance is ${formatAmount(bankBalance, "$")} and the price is ${formatAmount(total, "$")}</h2><br/>`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment