Submodule 7.3: Applying the "if" statements
The Temperature converter problem
Exercise
- Open your editor, create a new file and save it as
exersice07.3.03.html
in the folder "Exercises". - Copy the above code and paste it in the new file.
- Modify and save the file. The browser's output should be as shown in the following image:
var convertFtoC = document.getElementById("degF");
convertFtoC.onchange = function(){ /*onchange means that every time the value in the input box changes,
this function will run*/
var degreesF = document.getElementById("degF").value; /* this is the value from the form field*/
if (isNaN(degreesF)) { /* Here we check if the input is a number*/
document.getElementById("degCOut").innerHTML = "I can't convert to"
} else {
var degreesC = (degreesF-32)*(5/9); /* you will set this to the results of your conversion*/
document.getElementById("degCOut").innerHTML = degreesC;
}
}