In this project, you’ll create a JavaScript that calculates the Celsius equivale
ID: 3861573 • Letter: I
Question
In this project, you’ll 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 today’s date in the comment section in the document head.
2. At the bottom of the document, before closing </body> Tag, enter <script>, insert a blank line, and then enter </script> 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 </script> 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 -40°C.
Explanation / Answer
Solution:-
Here the HTML program is given below slightly different way. It simply takes a degree Fahrenheit temperature value and returns a degree Celsius temperature value.
It takes inputs in a form and convert function is defined in script.
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
Matt Thomas </br>
Date : 15 March 2017
<title>Temperature Converter</title>
</head>
<body>
<p>This is a temperature Converter program </p>
<script type="text/javascript">
function convertToC() {
var fTempVal = parseFloat(document.getElementById('fTemp').value);
var cTempVal = (fTempVal - 32) * (5 / 9);
document.getElementById('cTemp').value = cTempVal;
return false;
}
</script>
<form name="conversionForm">
<table border="1">
<tbody>
<tr>
<td>Fahrenheit</td>
<td>
<input name="fTemp" id="fTemp" type="text" />
</td>
<td>
<button type="button">Convert to Celsius</button>
</td>
</tr>
<tr>
<td>
<input name="cTemp" id="cTemp" type="text" />
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.