Why do I get NaN from I function in JavaScript -
i have simple javascript program converts celsius fahreinheit , vice versa. 1 function not work , returns nan.
<script type="text/javascript"> function ctof() { c = parseint(document.getelementbyid("celsiustemp").value); fah = c * (9 / 5) + 32; document.getelementbyid("resc").innerhtml = fah; } function ftoc() { f = parseint(document.getelementbyid("celsiustemp").value); cel = (f - 32) * (5 / 2); document.getelementbyid("resf").innerhtml = cel; } </script> <body> <form> <p>insert celsius temperatur: <input type="text" id="celsiustemp" /> <br> </p> <p>insert fahrenheit temperatur: <input type="text" id="fahreheittemp" /> <br> </p> <input type="button" class="btn btn-success" onclick="ctof()" value="calc fahrenheit" /> <input type="button" class="btn btn-warning" onclick="ftoc()" value="calc celsius" /> </form> <br/> <p>the result : <br/> <p>result celsius fahreinhet:</p><span id="resc"></span> <p>result fahreinhet celsius:</p><span id="resf"></span> </body>
why nan when calculating celsius)
thanks!!
you need replace:
f = parseint(document.getelementbyid("celsiustemp").value);
with:
f = parseint(document.getelementbyid("fahreheittemp").value);
if click calc celsius
button, try value celsius field. if that's empty, parseint
return nan
:
isnan(parseint("")) === true
Comments
Post a Comment