[英]How do I get rid of the NaN error when converting temperature with a function in JavaScript?
I have been trying to make a JS Fahrenheit to Celsius converter, and for some reason whenever I input a number and that goes through the function, it spits back out a NaN
where the Temperature is supposed to go.我一直在尝试制作一个 JS 华氏温度到摄氏温度的转换器,出于某种原因,每当我输入一个数字并经过 function 时,它会吐出一个NaN
,其中温度应该为 go。
Edit: Added in the "value" after the "ftemp" and "ctemp"编辑:在“ftemp”和“ctemp”之后的“值”中添加
Edit: I deleted the values in the ftemp and ctemp and instead tried to access them in the function itself just like two people answered, and it works fine now.编辑:我删除了 ftemp 和 ctemp 中的值,而是尝试在 function 本身中访问它们,就像两个人回答的那样,现在工作正常。
function ftoc(ft) { c = ft * (9 / 5) + 32; return c; } function ctof(ct) { f = (c - 32) / (9 / 5); return f; } button1 = document.getElementById("ftempsubmit"); button2 = document.getElementById("ctempsubmit"); fvalue = document.getElementById("ftemp").value; cvalue = document.getElementById("ctemp").value; fpopup = document.getElementById("fpopup"); cpopup = document.getElementById("cpopup"); button1.onclick = function() { cpopup.value = ftoc(fvalue); }; button2.onclick = function() { fpopup.value = ctof(cvalue); };
<input type="text" id="ftemp" placeholder="Enter F" /> <button id="ftempsubmit">Submit F</button> <br /> <br /> <input id="cpopup" /> <br /> <br /> <input type="text" id="ctemp" placeholder="Enter C" /> <button id="ctempsubmit">Sumbit C</button> <br /> <br /> <input id="fpopup" />
You are not sending the actual value
of the inputs but rather the HTML element to your function which will throw NaN
您不是将输入的实际value
发送,而是将 HTML 元素发送到您的 function,这将抛出NaN
I have also change the type=text
to type=number
as you will using integer
only for conversion
.我还将type=text
更改为type=number
,因为您将仅使用integer
进行conversion
。
In your function ctof
you are getting argument at ct
but you are using c
only when doing the calculation which mean it will throw an error of c
not defined.在您的 function ctof
中,您在ct
处获得参数,但仅在进行计算时才使用c
,这意味着它将抛出c
未定义的错误。
Live Demo:现场演示:
function ftoc(ft) { var c = ft * (9 / 5) + 32; return c; } function ctof(ct) { var f = (ct - 32) / (9 / 5); return f; } button1 = document.getElementById("ftempsubmit"); button2 = document.getElementById("ctempsubmit"); fvalue = document.getElementById("ftemp"); cvalue = document.getElementById("ctemp"); fpopup = document.getElementById("fpopup"); cpopup = document.getElementById("cpopup"); button1.onclick = function() { cpopup.value = ftoc(+fvalue.value); }; button2.onclick = function() { fpopup.value = ctof(+cvalue.value); };
<!DOCTYPE html> <html> <head></head> <body> <input type="number" id="ftemp" placeholder="Enter F" /> <button id="ftempsubmit">Submit F</button> <br /> <br /> <input id="cpopup" /> <br /> <br /> <input type="number" id="ctemp" placeholder="Enter C" /> <button id="ctempsubmit">Sumbit C</button> <br /> <br /> <input id="fpopup" /> <br /> <br /> <script> </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.