In: Computer Science
7.14 LAB: Temperature conversion
In this lab, you will implement a temperature converter. Five UI elements are declared for you in the template:
Element's ID | Element description |
---|---|
cInput | Text input field for Celsius temperature |
fInput | Text input field for Fahrenheit temperature |
convertButton | Button that, when clicked, converts from one temperature to the other |
errorMessage | Div for displaying an error message when temperature cannot be converted |
weatherImage | Image corresponding to the temperature |
Implement the conversion functions (2 points)
Implement the convertCtoF() and convertFtoC() functions to convert between Celsius and Fahrenheit. convertCtoF() takes a single numerical argument for a temperature in Celsius and returns the temperature in Fahrenheit using the following conversion formula:
°F = °C * 9/5 + 32
Similarly, convertFtoC() takes a single numerical argument for a temperature in Fahrenheit and returns the temperature in Celsius using the following conversion formula:
°C = (°F - 32) * 5/9
Register conversion button's click event in domLoaded() (2 points)
When the DOM finishes loading, the domLoaded() function is called. Implement domLoaded() to register a click event handler for the Convert button (id="convertButton"). Use addEventListener(), not onclick.
When the Convert button is pressed, the text box that contains a number should be converted into the opposing temperature. So if a number is in the Celsius text box (id="cInput"), the temperature should be converted into Fahrenheit and displayed in the Fahrenheit text box (id="fInput") and vice versa. Use parseFloat() to convert from a string to a number and do not round the result.
Ensure that only one text field contains a value (2 points)
Ensure that only one text field contains a value at any moment in time unless the Convert button has been pressed. Ex: When the Celsius field has a number and the user enters a Fahrenheit entry, the Celsius field should be cleared as soon as the user begins to type. This will require implementing an input event handler for each of the text fields that clears the opposing text field when a change occurs. Register each input event handler in the domLoaded() function. Use addEventListener(), not oninput.
Change the image to reflect the temperature (2 points)
When the temperature is converted, change the image to reflect the temperature in Fahrenheit. Each image is in the same directory as your .html page.
Below 32 F | 32 - 50 F | Above 50 F |
cold.gif | cool.gif | warm.gif |
Handle bad input (2 points)
When parseFloat() returns a NaN for the temperature to be converted, set errorMessage's innerHTML to the message: "X is not a number", where X is the string from the text input. When parseFloat() returns a valid number, set errorMessage's innerHTML to an empty string. The image below shows a sample error message.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
CODE GIVEN:
index.html
<!DOCTYPE html>
<html lang="en">
<title>Temperature Converter</title>
<script src="convert.js"></script>
<style>
label {
display: block;
}
#errorMessage {
color: red;
}
</style>
<body>
<p>
<label for="cInput">Celsius:</label>
<input id="cInput" type="text">
</p>
<p>
<label for="fInput">Fahrenheit:</label>
<input id="fInput" type="text">
</p>
<input id="convertButton" type="button"
value="Convert">
<div id="errorMessage">
</div>
<p>
<img id="weatherImage" src="warm.gif" alt="Warm">
</p>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------
convert.js
window.addEventListener("DOMContentLoaded", domLoaded);
function domLoaded() {
// TODO: Complete the function
}
function convertCtoF(degreesCelsius) {
// TODO: Complete the function
}
function convertFtoC(degreesFahrenheit) {
// TODO: Complete the function
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
OTHER FILES:
cold.gif || warm.gif || cool.gif ||
convert.js
window.addEventListener("DOMContentLoaded", domLoaded);
function domLoaded() {
var cInput = document.getElementById("cInput");
var fInput = document.getElementById("fInput");
cInput.addEventListener("input", function(){ //on input into celcius input clears the input in fahrenheit input
document.getElementById("fInput").value = "";
}, false);
fInput.addEventListener("input", function(){ //on input into fahrenheit input clears the input in celcius input
document.getElementById("cInput").value = "";
}, false);
var convertButton = document.getElementById("convertButton");
convertButton.addEventListener("click", function(){ //adding click event listener
var cInput = document.getElementById("cInput");
var fInput = document.getElementById("fInput");
var errMsg = document.getElementById("errorMessage");
var img = document.getElementById("weatherImage");
function updateImage(degreesFahrenheit) //updates the image basaed on the parameter passed
{
if(degreesFahrenheit<32)
{
img.src = "cold.gif";
img.alt = "Cold";
}
else if(degreesFahrenheit>=32 && degreesFahrenheit<=50)
{
img.src = "cool.gif";
img.alt = "Cool";
}
else
{
img.src = "warm.gif";
img.alt = "Warm";
}
}
var degreesCelsius = parseFloat(cInput.value); //parsing from celcius input
var degreesFahrenheit = parseFloat(fInput.value); //parsing from fahrenheit input
//one of the above two is NaN
if(cInput.value==="") //if celcius input is clear which means user entered fahrenheit input
{
if(isNaN(degreesFahrenheit)) //if degreesFahrenheit is Not a Number (NaN) then its invalid input so printing error msg
errMsg.innerHTML = fInput.value+"is not a number";
else //in the else case
{
cInput.value = convertFtoC(degreesFahrenheit); //converting fahrenheit to celcius and updating celsius input
errMsg.innerHTML = ""; //clearing the error message which if so was updated previously
updateImage(degreesFahrenheit); //updating the image
}
}
if(fInput.value==="") //if fahrenheit input is clear which means user entered celcius input
{
if(isNaN(degreesCelsius)) //if degreesCelsius is Not a Number then printing the error msg
errMsg.innerHTML = cInput.value+"is not a number";
else //in the else case
{
fInput.value = convertCtoF(degreesCelsius); //converting celcius to Fahrenheit and updating the other input
errMsg.innerHTML = ""; //clearing previous error msg if any
updateImage(convertCtoF(degreesCelsius)); //updating the image based on fahrenheit value
}
}
}, false);
}
function convertCtoF(degreesCelsius) { //function to convert celcius to fahrenheit
return degreesCelsius*9/5 + 32;
}
function convertFtoC(degreesFahrenheit) { //function to convert fahrenheit to celcius
return (degreesFahrenheit-32)*5/9;
}
convert.js Screenshots
index.html Screenshot (No changes made)
Output Screenshots
Printng error message when input is not a number
Observe the image alt message as 90F > 50F and src is warm.gif
Observe as 30F < 32F image alt is Cold and src is cold.gif
Observe that since 32F<=48F<=50F the image alt is Cool and src is cool.gif
(Regarding the output image)Since those images are with you, simple add them to the directory in which this html and js files are present and they will work just fine.
Each and everything is explained within the comment section of the content.js script.
If you like my work, don't forget to hit like. Thank you!
It has been pleasure helping you :)