Μετατροπέας θερμοκρασίας

Άσκηση

  1. Ανοίξτε τον επεξεργαστή κώδικα, δημιουργήστε ένα νέο αρχείο και αποθηκεύστε το ως exersice07.3.03.html στον φάκελο "Exercises".
  2. Αντιγράψτε τον ακόλουθο κώδικα και επικολλήστε τον στο νέο αρχείο.
  3. Επεξεργαστείτε το αρχείο. Στον browser θα πρέπει να εμφανίζεται η επόμενη εικόνα:

Temperature converter

Κώδικας:

Λύση:

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