Skip to content

Instantly share code, notes, and snippets.

@jgresalfi
Created September 4, 2016 20:23
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 jgresalfi/ca4e38cfe4eff609300759c3717c176a to your computer and use it in GitHub Desktop.
Save jgresalfi/ca4e38cfe4eff609300759c3717c176a to your computer and use it in GitHub Desktop.
Silly phone buying app...
"use strict";
const SALESTAX = .0975,
PHONE_PRICE = 200.00,
ACCESS_PRICE = 40.00;
var ba = prompt("How much money you got?"),
ac = prompt("When to stop buying accessories?"),
bankAccount = parseInt(ba),
accessoryLimit = parseInt(ac),
subTotal,
totalCost;
function salesTx(subtotal) {
subTotal = subTotal + (subTotal * SALESTAX);
return subTotal;
}
function phoneAndAccessory() {
subTotal = PHONE_PRICE + ACCESS_PRICE;
totalCost = salesTx(subTotal);
totalCost = parseInt(totalCost);
console.log("Price of phone and accessory with sales tax: " + "$" + totalCost.toFixed(2));
return totalCost;
}
function justPhone() {
subTotal = PHONE_PRICE;
totalCost = salesTx(subTotal);
totalCost = parseInt(totalCost);
console.log("Price of just phone with tax: " + "$" + totalCost.toFixed(2));
return totalCost;
}
function buySomeStuff() {
do {
if (bankAccount > accessoryLimit) {
phoneAndAccessory();
bankAccount = (bankAccount - totalCost).toFixed(2);
console.log("Phone and Accessory purchased, bank account balance: " + "$" + bankAccount);
bankAccount = parseInt(bankAccount);
} else if (bankAccount < accessoryLimit && bankAccount >= totalCost) {
justPhone();
bankAccount = (bankAccount - totalCost).toFixed(2);
console.log("Just a phone purchased, bank account balance: " + "$" + bankAccount);
bankAccount = parseInt(bankAccount);
}
} while (bankAccount > totalCost)
if (bankAccount <= totalCost) {
console.log("Insufficient funds: " + "$" + bankAccount.toFixed(2));
}
}
buySomeStuff();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment