CS 10 Homework 7

Due by the beginning of class Thursday, March 13

The JavaScript examples we developed in class (the days-until-Christmas calculator, the temperature converter, and the leap year program) can be found in the CS 10 class folder under Lectures/week07. Use these examples as a guide in writing your programs for this assignment.

  1. The Santa.html program that we developed in class displays the current date and calculates the number of days remaining until Christmas. Unfortunately, it displays the date in a rather hard-to-read format:

    Today is Thu Mar 6 18:22:26 PST 2003

    Modify the program to display the date in the nicer format shown below:

    Today is Thursday, March 6, 2003

    To do this, you will need to use the getDay(), getMonth(), getDate(), and getFullYear() methods to extract the relevant information from the today date object (see pages 8.14-18 of your HTML book for details). You will also need to convert the month from a number (0 to 11) into a month name ("January", "February", etc.), and the weekday from a number (0 to 6) into a weekday name ("Sunday", "Monday", etc.). Your program should use if/else statements to accomplish this. Do not use functions or arrays for this assignment, as shown in Tutorial 8. We'll get to functions and arrays soon enough.

  2. Create a Web page called SalesTax.html that prompts the user for the name of a state and a dollar amount when the page is loaded, calculates the sales tax for that amount, and displays the grand total. If the user enters either "California" or "CA" for the state, the program should use a sales tax rate of 0.08, otherwise it should use 0.05. For example, if the user enters "California" and "200", the program should display "Total: $216". If the user enters "CA" and "100", the program should display "Total: $108". If the user enters "Texas" and "100", the program should display "Total: $105". The formula for calculating sales tax is simply:

    total = amount + tax-rate × amount

  3. Create a Web page called Quadratic.html that calculates the roots of the quadratic equation ax2 + bx + c = 0, using the formula shown below. This formula produces two values, depending on whether + or - is used in place of the + symbol:

    Your program should prompt the user for three different values (a, b, and c) when the page is loaded. After the third value is entered, the program should calculate the two roots and display them on the page with an appropriate message. For example, with the values a = 3, b = 10, and c = 3, the two roots should be -0.33333 and -3. With a = 4, b = 12, and c = 9, both roots should be -1.5. What happens if you enter a = 1, b = 0, and c = 1?

    Note: JavaScript includes a built-in operation called Math.sqrt(n) that can be used to calculate the square root of n. For example, Math.sqrt(9) will evaluate to 3, since the square root of 9 is 3.

  4. Modify your Quadratic program so that it displays an appropriate error message if an attempt is made to compute the square root of a negative number.

Turning in your homework

Put your Santa.html, SalesTax.html, and Quadratic.html files into a folder called Your name HW 7 and drop it into the CS 10 drop folder.