Question: In this project, youll create a JavaScript that calculates the Celsius equivalent of a Fahrenheit temperature. Note that your results should work on all modern

In this project, youll create a JavaScript that calculates the Celsius equivalent of a Fahrenheit temperature. Note that your results should work on all modern browsers, but it will not work on IE8 or previous version of IE.

1. In your text editor, open index.html that you just created. Enter your name and todays date in the comment section in the document head.

2. At the bottom of the document, before closing Tag, enter to create a new script section.

3. Within the script section you created in the previous step, enter the following functions.

function convert() {

var degF = document.getElementById(fvalue).value;

var degC = degF 32*5/9;

document.getElementById(cValue).innerHTML =degC;

}

This function, named convert(), starts by looking up the Fahrenheit value entered by the users and assigning it to a variable named degF. It then performs caluculations on degF to arrive at the Celsius equivalent, which is assigned to a variable named degC. Finally, it assigns the value of degC as the innerHTML value of the element with the id value cValue.

4. Below the closing } for the convert() function, but before the closing tag, enter the following statement to add an event listener:

document.getElementById(button).

addEventListener(click, convert, false);

5. Save your work, open index.htm in your browser, enter -40 in the Enter temp in F box, and they click the Convert to C button. -40 Fahrenheit is actually equivalent to -40 Celsius. However, the formula incorrectly calculates that -40 F is equivalent to -57.7 C. This is because the calculations in the equation must take place in a different order than the order of precedence dictates.

6. Return to the index.htm file in your text editor, and then add two sets of parentheses to the first statement in the convert() function to modify the order in which the calculations are performed, as the follows:

var degC = (degF -32) * (5/9);

7. Save your work, refresh or reload index.hlm in your browser, enter -40 in the Enter temp in F box, and then click the Covert to C button. As Figure 2-18 shows, the temperature is now calculated correctly as -40C.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!