<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);
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.