Assignment 8

Back to Lesson # 8- Looping Table of Contents # 8 Review Questions

 

For these exercises, you will need a blank web page to start. You will create three buttons, one for each exercise, and some functions in JavaScript to serve as event handlers when the user clicks each button.

1. 'For' loops are the best choice for times when we already know (or can easily calculate the number of repetitions you want the computer to do. Use a 'For' loop and write an event handler. Put a button on the web page that says "Blast Off", and have the event handler display a window that counts backwards from 10 to zero, with each number on its own line. At the end - print "Blast Off"!

2. Pre-Test loops are coded with the 'while(...){ } style of loop. They may execute 0 times if the condition is not true the first time the script tests it. Have you ever visited a web site that asks you for a serial number of a product before letting you in to certain areas of the site? Annoying as it may be, it is necessary sometimes to check that a user is actually supposed to get somewhere, before letting her or him in. Write a web page with a text box that prompts for a serial number. Pretend that for this product, all serial numbers must start with the letters 'LUX' and have a total overall length of 7,. For example 'LUX1234' would be a valid serial number but both 'LUx1234' and 'LUX99' are not valid.

If the user enters a BAD serial number, then display an alert message "Invalid, please Re-Check and Re-Enter.". Keep displaying this until the user types in a GOOD serial number. At that point, an actual web site would open up a new window to the product support information. In this case you can just display an alert message that says "Thank You."

3. Ever buy something on a charge card, and just pay the minimum each month? Heh, heh, that's is what the credit companies want you to do, since that is how they get their mitts on your money. Say you buy something cool (use your imagination) for $100. Most credit cards have no charge (grace period) for the first statement's due date, so then you make a minimum payment of $10. At that point, they will add on one twelfth of the annual interest charge, which you will see added next month. That continues until the balance is 0. Write a web page to calculate the amount you actually pay if you make the minimum payment each time. Start with a form containing a text box that accepts the price of your purchase, and another text box that accepts the annual interest rate (use 18% as the default rate). Then have the page call a JavaScript function that calculates the minimum payment as 10% of the original total.

Follow these steps - this is the 'algorithm' or step by step breakdown: I suggest making a separate function called 'roundMoney()' just to round the numbers.

function receives amount, rate as passed in values


minPayment=amount * .10 or $10, whichever is less

balance=amount
totalCharge=0

month=1


do 

   if month 1:  charge=0
   else
      charge=rate/12 * balance
      balance = balance + charge
      totalCharge=totalCharge + charge
      if balance < minPayment then minPayment = balance // don't have to pay more than u owe

   end of if-else
   
   //right here, all the numbers need to be rounded to 2 places or will look UGLYYYYY
   output month, balance, charge, minPayment, totalCharge

   month = month + 1
   balance = balance - minPayment

while (balance >=0)


end function

Just a note here - maybe your user would not want to see all that detail, just the total of all the charges. You could certainly do that

Return to Top of Page | Back to Main Menu