Saturday, January 7, 2017

01/07/2017 YDKJS- Exercises

https://github.com/getify/You-Dont-Know-JS/blob/master/up%20%26%20going/ch1.md

Today I worked through these exercises using the console of an about:blank webpage in order to get hands on with Javascript programming.

Some notes to self:
Use Shift + Control+ I to access the Console

By convention, JavaScript variables as constants are usually capitalized, with underscores _ between multiple words.
The newest version of JavaScript at the time of this writing (commonly called "ES6") includes a new way to declare constants, by using const instead of var

The for loop has three clauses: the initialization clause (var i=0), the conditional test clause (i <= 9), and the update clause (i = i + 1). So if you're going to do counting with your loop iterations, for is a more compact and often easier form to understand and write.

Practice: 
  • 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!
1. set up constants and variables

const TAX_RATE= 0.08;
const PHONE_PRICE = 99.99
const ACCS_PRICE = 9.99;
const SPENDING_THRESHOLD = 300; 
var bank_balance= 400; 
var amount = 0;

2. Define functions

a. calculating tax 

function calculateTax(amount) {
      return amount *TAX_RATE;
}

b. formatting price with $ and rounding to two decimal places

function formatAmount(amount) {
      return "$" + amount.toFixed(2); 
}

3. now we purchase phones until we run out of money in bank account (this runs as a loop) 

while (amount < bank_balance) {
amount = amount + PHONE_PRICE;  // if we have more money in the bank than the cost, we will buy the phone

if (amount < SPENDING_THRESHOLD) {
amount = amount + ACCS_PRICE; //if the price is still less that what we want to spend, we will get a charger
}
}

amount = amount + CalculateTAX(amount); //then, we have to calculate the final amount which is the total plus tax 

console.log ("Your total is: " + formatAmount(amount)); //this spits out our grand total 

if (amount > bank_balance) {
console.log("Don't buy it!");  //otherwise, this spits out that we shouldn't buy
}




No comments:

Post a Comment