HW5 Solutions EXERCISE 5.5: Define a page named ctof.html that is similar to your ftoc.html page but performs the opposite conversion. This means that the page should prompt the user for a temperature in degrees Celsius and then convert that temperature to Fahrenheit using the formula: tempInFahr = ((9/5) * tempInCelsius) + 32; Once you have completed the page, use it to convert the following temperatures: 0 Celsius 20 Celsius -10 Celsius 88 Celsius ANS: <html> <!-- ctof.html Dave Reed --> <!-- Converts a temperature from Celsius to Fahrenheit. --> <!-- ================================================== --> <head> <title>Celsius to Fahrenheit</title> </head> <body> <h3 style="text-align:center">Temperature Conversion Page</h3> <script type="text/javascript"> tempInCelsius = prompt("Enter the temperature (in Celsius):", "100"); tempInCelsius = parseFloat(tempInCelsius); tempInFahr = ((9/5) * tempInCelsius) + 32; document.write("<p>You entered " + tempInCelsius + " degrees Celsius.</p>"); document.write("<p>That's equivalent to " + tempInFahr + " degrees Fahrenheit.</p>"); </script> </body> </html> ------------------------------------------------------------------------------- 0 Celsius 32 Fahrenheit 20 Celsius 68 Fahrenheit -10 Celsius 14 Fahrenheit 88 Celsius 190.4 Fahrenheit EXERCISE 5.6: Create a Web page named grades.html that can be used to compute a student’s overall average for a course. Your page should prompt the user to enter his or her homework average, lab average, midterm score, and final exam score. The page should store each value as a number in a separate variable and then use the four numbers to compute the overall course average. Assume the following grade weightings: Homework 25% Labs 20% Midterm 25% Final exam 30% To compute the overall course average, scale each individual grade by the appropriate factor and then add the four values. The following expression demonstrates the necessary calculation. average = homework*0.25 + labs*0.20 + midterm*0.25 + exam*0.30; The page should display the grades entered by the student, as well as the overall average.Your page should appear similar to the sample in Figure 5.5. ANS: <html> <!-- grades.html Dave Reed --> <!-- This page computes and displays grades for a course. --> <!-- ==================================================== --> <head> <title> Grade Calculator </title> </head> <body> <script type="text/javascript"> homework = prompt("Enter your homework average", 90); homework = parseFloat(homework); labs = prompt("Enter your lab average", 90); labs = parseFloat(labs); midterm = prompt("Enter your midterm grade", 90); midterm = parseFloat(midterm); exam = prompt("Enter your exam grade", 90); exam = parseFloat(exam); average = homework*0.25 + labs*0.20 + midterm*0.25 + exam*0.30; document.write("<p>homework average: " + homework + "<br />"); document.write("lab average: " + labs + "<br />"); document.write("midterm grade: " + midterm + "<br />"); document.write("exam grade: " + exam + "</p>"); document.write("<p>overall course average: " + average + "</p>"); </script> </body> </html> EXERCISE 5.11: Trace the execution of the following JavaScript code and try to predict its behavior. For each assignment, fill in the values of the variables in their corresponding boxes—even those values that aren't changed. For each write statement, list the output that would be displayed. word1 = "foo"; "foo" word1 word2 = "foo" + "bar"; "foo" "foobar" word1 word2 document.write("<p>word1 = " + word1 + ", word2 = " + word2 + "</p>"); word1 = foo, word2 = foobar word3 = "biz"; "foo" "foobar" word1 word2 word1 = word2 + word3; "foobarbiz" "foobar" word1 word2 word2 = word2 + word2; "foobarbiz" "foobarfoobar" word1 word2 document.write("<p>word1 = " + word1 +", word2 = " + + word3 + "</p>"); word1 = foobarbiz, word2 = foobarfoobar, word3 = biz "biz" word3 "biz" word3 "biz" word3 word2 + ", word3 = " Verify your predictions by entering the above code into a Web page (within SCRIPT tags) and loading that page in the browser. Do the write statements produce the messages you expected? EXERCISE 5.15: Trace the execution of the following JavaScript code and try to predict its behavior. Be sure to differentiate between string values and numeric values in the memory cells by enclosing all string values in quotes. Assume that the user enters the value 1024 at the prompt. x = prompt("Enter a number:", ""); "1024" x document.write("<p>Double " + x + "is " + (2 * x) + "</p>"); Double 1024 is 2048 document.write("<p>Double " + x + "is " + (x + x) + "</p>"); Double 1024 is 10241024 x = parseFloat(x); 1024 x document.write("<p>Double " + x + "is " + (x + x) + "</p>"); Double 1024 is 2048 Verify your predictions by entering the above code into a Web page (within SCRIPT tags) and loading that page in the browser. Do the write statements produce the messages you expected? EXERCISE 5.16: Create a Web page named times.html that prompts the user to enter a number of seconds and stores that number in a variable. Your page should use the method outlined in this section to break that number of seconds into hours, minutes, and seconds. You should also include a write statement that displays the time in hours, minutes, and seconds. For example, if the user enters 12345 at the prompt, the page should display: That's equivalent to 3 hours, 25 minutes, and 45 seconds. Use your page to convert each of the following time intervals: 20 seconds 500 seconds 86400 seconds 123456 seconds <html> <!-- times.html Dave Reed --> <!-- This page converts seconds to days, hours, minutes and seconds. --> <!-- =============================================================== --> <head> <title> Seconds Converter</title> </head> <body> <script language="JavaScript"> seconds = prompt("Enter a number of seconds:", "60"); seconds = parseFloat(seconds); hours = Math.floor(seconds / (60*60)); seconds = seconds % (60*60); minutes = Math.floor(seconds / 60); seconds = seconds % 60; document.write("<p>That's equivalent to " + hours + " hours, " +minutes + " minutes, and " + seconds + " seconds.</p>"); </script> </body> </html> ------------------------------------------------------------------------That's equivalent to 0 hours, 0 minutes, and 20 seconds. That's equivalent to 0 hours, 8 minutes, and 20 seconds. That's equivalent to 24 hours, 0 minutes, and 0 seconds. That's equivalent to 34 hours, 17 minutes, and 36 seconds.