2017-10-05 2 views
-4

Ich habe ein Problem mit einer meiner Javascript-Funktionen gerannt. Ich versuche, eine ganze Zahl durch eine Ganzzahl zu teilen und NaN als Ergebnis zu erhalten. Ich habe verschiedene mögliche Lösungen gelesen und ausprobiert, konnte aber nicht die richtige Antwort finden. Bitte sehen Sie sich den folgenden Code an (laden Sie ihn, wenn Sie wollen) und lassen Sie mich wissen, wie ich es beheben kann, wenn Sie können. DankDivision Equation: gibt NaN zur Zeit

<!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> 

Antwort

2

Zuerst lesen Sie bitte dazu, wie ein Minimal, Complete, and Verifiable Example zu erstellen. Hier gibt es eine Menge Unordnung.

Zweitens ist Ihr Problem ziemlich offensichtlich. Sie haben das Problem genug isoliert, dass Sie wissen, dass es mit der Division zu tun hat. Ihre einzige Abteilung ist in diesen Zeilen:

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

Der Wert bexaRollBoxVar ist der Wert der Eingabe mit der ID bexaRoll. Dieser Wert ist "TRADE". Nichts von deinem Code ändert das jemals. Ihre dritte Zeile wird also durch eine Zeichenfolge und nicht durch eine Ganzzahl geteilt. Also der Fehler.

Ich sollte darauf hinweisen, dass Sie das gleiche Problem mit altraRollBoxVar haben. Sie teilen eine Zeichenfolge tatsächlich durch eine Zeichenfolge. Es überrascht nicht, dass das nicht funktioniert.

Schon ein minimaler Versuch, dieses zu debuggen, wie alert(bexaRollBoxVar);, hätte das Problem aufgedeckt. In Zukunft sollten Sie sich bemühen, das Problem selbst zu lösen, bevor Sie hier posten.