Part A of the Unit 2 Project:

Create a mathematical converter for temperature

by Angela Baruth

HTML

<p>If the user types in values that are not numbers, do nothing.</p>
      <p>Remember that the formula for converting temperature looks like this:</p>
      <span style="font-family: monospace;">°C x 9/5 + 32 = °F<br>
        <br>
        (°F - 32) x 5/9 = °C</span><br><br>
     
     <p>Insert a number into one of the input fields below:</p>
<input id="degC" onKeyUp="convert('C')"> degrees Celsius<br>
equals<br>
<input id="degFOut" onKeyUp="convert('F')"> degrees Fahrenheit

<p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p>
        
      <p><br>
      </p>

JavaScript

/********************************************************************
  *
  * First problem: temperature conversion
  *
  * If the values entered by the user aren't numbers (or convertible to numbers),
  * return nothing (or, more specifically, leave the output field blank)
  *
  ********************************************************************/
  

function convert(degree) {
    if (degree == "C") {
        F = document.getElementById("degC").value * 9 / 5 + 32;
        document.getElementById("degFOut").value = Math.round(F);
    } else {
        C = (document.getElementById("degFOut").value -32) * 5 / 9;
        document.getElementById("degC").value = Math.round(C);
    }
}