rabbitrun
9/4/2016 - 8:23 PM

Silly phone buying app...

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();