Submodule 7.3: Applying the "if" statements

The Temperature converter problem

Exercise

  1. Open your editor, create a new file and save it as exersice07.3.03.html in the folder "Exercises".
  2. Copy the above code and paste it in the new file.
  3. Modify and save the file. The browser's output should be as shown in the following image:

Temperature converter

Code:

Solution:

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;
    }
}