Skip to content

Instantly share code, notes, and snippets.

@hiddenpearls
Created April 27, 2017 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiddenpearls/28ec6eb38638dbc2af08dd8e4e20ef70 to your computer and use it in GitHub Desktop.
Save hiddenpearls/28ec6eb38638dbc2af08dd8e4e20ef70 to your computer and use it in GitHub Desktop.
Simple JS program for buying a phone, Apply tax, format and add to cart.
const TAX_RATE = 0.10; // 10%
const PHONE_PRICE = 500; // $500
const ACCESSORY_PRICE = 12; //$12
const SPENDING_THRESHOLD = 900; // This should be 60% of the total bank balance.
var bank_account_balance = 0;
var quantity = 0;
var total = 0;
bank_account_balance = prompt("What is your Bank Account Balance");
quantity = prompt("One phone costs " + PHONE_PRICE + " How many you want to buy");
console.log(buy(PHONE_PRICE, quantity));
function buy( product, qty ){
total = product * qty;
total += tax_calculation(total)
if( total > SPENDING_THRESHOLD){
return "Warning! This is a threshold alert";
}else if( total > bank_account_balance){
return "Warning! You can't buy this phone. Please check your bank";
}else{
return "Your Purchase: " + format_price(total);
}
}
function tax_calculation( amount ) {
return amount * TAX_RATE;
}
function format_price( amount ){
return '$' + amount.toFixed(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment