【问题标题】:Division Equation: returns NaN currently除法方程:当前返回 NaN
【发布时间】:2017-10-05 12:23:48
【问题描述】:

我的一个 javascript 函数遇到了问题。我试图将一个整数除以一个整数并不断得到 NaN 作为结果。我已经阅读并尝试了各种可能的解决方案,但一直无法找到正确的答案。请查看以下代码(如果需要,请加载它),如果可以,请告诉我如何修复它。谢谢

<!doctype html>
<html>
<style>
*:focus {
outline: none;
}
@font-face {
font-family: systems_analysis;
src: url(systems_analysis.ttf);
}
@font-face {
font-family: eufont;
src: url('eufont.ttf');
}
.button {
background-color: white; 
color: black;
border-radius: 6px;
border: none;
cursor: pointer;
width: 400px;
height: 20px;
font-size: 15px;
font-family: systems_analysis;
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.2), 0 1.5px 5px 0 rgba(0,0,0,0.19);
}
.button:hover {
background-color: black; 
color: white;
}
.button:active {
transform: translateY(1px);
}
.button:focus {
outline:0 !important;
}
.balanceBox {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 400px; 
}
.priceBoxAltra {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 225px;
}
.priceBoxBexa {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 225px;
}
body {
background-image: url("risefx_background.jpg");
}
</style>
<head>
<title>RiseFX</title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center>
<span><input type="text" id="altraPrice" class="priceBoxAltra" value="1.00000"></span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;
<span><input type="text" id="bexaPrice" class="priceBoxBexa" value="1.00000"></span>
</center>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center>
<input type="text" id="fxPrice" class="priceBoxAltra" value="0.00000">
</center>
<br>
<center>
<span><input type="button" id="altraRoll" class="button" value="TRADE" onclick="altraFunction()"></span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
<span><input type="button" id="bexaRoll" class="button" value="TRADE" onclick="bexaFunction()"></span>
</center>
<br>
<br>
<center>
<input type="text" id="balance" class="balanceBox" value="1.0000000000" readonly>
</center>
<script>
    function altraFunction() {
             var altraRollVar = (Math.random()*2);

             document.getElementById("altraPrice").value = altraRollVar.toFixed(5);
             fxPriceConversion();

}
</script>
<script>
    function bexaFunction() {
             var bexaRollVar = (Math.random()*2);

             document.getElementById("bexaPrice").value = bexaRollVar.toFixed(5);
             fxPriceConversion();
}
</script>
<script>
    function fxPriceConversion() {
             var altraRollBoxVar = document.getElementById("altraRoll").value;
             var bexaRollBoxVar = document.getElementById("bexaRoll").value;
             var conversion = altraRollBoxVar / bexaRollBoxVar;

             document.getElementById("fxPrice").value = conversion.toFixed(5);
}
</script>

</body>
</html>

【问题讨论】:

    标签: javascript html css equation


    【解决方案1】:

    首先,请阅读如何创建Minimal, Complete, and Verifiable Example。这里有很多混乱。

    其次,你的问题很明显。您已经将问题隔离得足够多,以至于您知道它与分裂有关。您唯一的部门是以下几行:

    var altraRollBoxVar = document.getElementById("altraRoll").value;
    var bexaRollBoxVar = document.getElementById("bexaRoll").value;
    var conversion = altraRollBoxVar / bexaRollBoxVar;
    

    bexaRollBoxVar 的值是 id 为 bexaRoll 的输入的值。该值为"TRADE"。您的任何代码都不会改变这一点。因此,上面的第三行是除以字符串,而不是整数。因此错误。

    我应该指出您对altraRollBoxVar 有同样的问题。您实际上是在将字符串除以字符串。不出所料,这行不通。

    即使是最小的调试尝试,例如alert(bexaRollBoxVar);,也会发现问题。以后在发帖前请自己努力解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 2017-08-26
      • 2016-10-09
      • 2021-06-04
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多