Skip to content

Instantly share code, notes, and snippets.

@JPGITHUB1519
Created January 29, 2019 20:42
Show Gist options
  • Save JPGITHUB1519/32d3b079c5417aa40b13740b7a90bb09 to your computer and use it in GitHub Desktop.
Save JPGITHUB1519/32d3b079c5417aa40b13740b7a90bb09 to your computer and use it in GitHub Desktop.
Introduction to JavaScript Programming Course from Front-End Master. https://frontendmasters.com/courses/javascript-basics/
/* Exercise from https://github.com/getify/You-Dont-Know-JS/blob/master/up%20&%20going/ch1.md#practice */
(function() {
const TAX_RATE = 0.08;
const PHONE_PRICE = 99.99;
const ACCESORY_PRICE = 9.99;
const SPENDING_THRESHOLD = 200;
var accountBalance = 303.91;
var amount = 0;
function calculateTax(amount, taxRate) {
return amount * taxRate;
}
function numberFormat(number) {
return "$" + number.toFixed(2);
}
while (amount < accountBalance) {
amount += PHONE_PRICE;
if (amount < SPENDING_THRESHOLD) {
amount += ACCESORY_PRICE;
}
}
amount += calculateTax(amount, TAX_RATE);
console.log("Calculated Purchase: " + numberFormat(amount));
if (amount <= accountBalance) {
console.log("You can afford it!");
} else {
console.log("You cannot afford it!");
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment