我将尽力在这里描述我的问题。我的js代码应该从if语句为手和筹码变量分配一个值。然后将其传递给另一个函数,该函数根据掷硬币的情况对其应用乘法器。最后,它应该将结果写入页面,并在每次重置指针时保留分数的总数。
我相信我的问题是当我尝试执行var totalHand = (totalHand + handC);
并返回NaN时。我不知道下一步该怎么做,我的老师曾提到使用parseInt,但我不确定这是否可以解决问题或如何应用。
https://jsfiddle.net/armadadamra/swcL1d2m/7/
我非常感谢您的帮助,并请耐心等待,我只是在学习js的第一学期。谢谢
我认为问题出在哪里:
var totalHand = (totalHand + handC);
var greenChipTotal = (greenChipTotal + greenCC);
var redChipTotal = (redChipTotal + redCC);
var purpleChipTotal = (purpleChipTotal + purpleCC);
var blackChipTotal = (blackChipTotal + blackCC);
所有的js:
// images
var dice1 = ['<img src="images/dice1.png" name="diceOne" width="250" height="250">']
var dice2 = ['<img src="images/dice2.png" name="diceTwo" width="250" height="250">']
var dice3 = ['<img src="images/dice3.png" name="diceThree" width="250" height="250">']
var dice4 = ['<img src="images/dice4.png" name="diceFour" width="250" height="250">']
var dice5 = ['<img src="images/dice5.png" name="diceFive" width="250" height="250">']
var dice6 = ['<img src="images/dice6.png" name="diceSix" width="250" height="250">']
var loader = ['<img src="images/loading.gif" name="load" width="250" height="250">']
var coin1 = ['<img src="images/coin1.jpg" name="coinOne" width="250" height="250">']
var coin2 = ['<img src="images/coin2.jpg" name="coinTwo" width="250" height="250">']
// game vars
var totalA = 0;
var handA = 0;
var greenC = 0;
var redC = 0;
var purpleC = 0;
var blackC = 0;
function myRandoms() {
var ranA = Math.floor((Math.random() * 6) + 1);
var ranB = Math.floor((Math.random() * 6) + 1);
var ranC = Math.floor((Math.random() * 6) + 1);
var ranD = Math.floor((Math.random() * 6) + 1);
// dice display
// dice a
if (ranA==1){document.getElementById("tdDiceA").innerHTML = dice1;};
if (ranA==2){document.getElementById("tdDiceA").innerHTML = dice2;};
if (ranA==3){document.getElementById("tdDiceA").innerHTML = dice3;};
if (ranA==4){document.getElementById("tdDiceA").innerHTML = dice4;};
if (ranA==5){document.getElementById("tdDiceA").innerHTML = dice5;};
if (ranA==6){document.getElementById("tdDiceA").innerHTML = dice6;};
// dice b
if (ranB==1){document.getElementById("tdDiceB").innerHTML = dice1;};
if (ranB==2){document.getElementById("tdDiceB").innerHTML = dice2;};
if (ranB==3){document.getElementById("tdDiceB").innerHTML = dice3;};
if (ranB==4){document.getElementById("tdDiceB").innerHTML = dice4;};
if (ranB==5){document.getElementById("tdDiceB").innerHTML = dice5;};
if (ranB==6){document.getElementById("tdDiceB").innerHTML = dice6;};
// dice c
if (ranC==1){document.getElementById("tdDiceC").innerHTML = dice1;};
if (ranC==2){document.getElementById("tdDiceC").innerHTML = dice2;};
if (ranC==3){document.getElementById("tdDiceC").innerHTML = dice3;};
if (ranC==4){document.getElementById("tdDiceC").innerHTML = dice4;};
if (ranC==5){document.getElementById("tdDiceC").innerHTML = dice5;};
if (ranC==6){document.getElementById("tdDiceC").innerHTML = dice6;};
// dice d
if (ranD==1){document.getElementById("tdDiceD").innerHTML = dice1;};
if (ranD==2){document.getElementById("tdDiceD").innerHTML = dice2;};
if (ranD==3){document.getElementById("tdDiceD").innerHTML = dice3;};
if (ranD==4){document.getElementById("tdDiceD").innerHTML = dice4;};
if (ranD==5){document.getElementById("tdDiceD").innerHTML = dice5;};
if (ranD==6){document.getElementById("tdDiceD").innerHTML = dice6;};
//resets checkboxes, coin picture and saying
$( "#checkHeads" ).prop( "checked", false );
$( "#checkTails" ).prop( "checked", false );
$( "#checkSkip" ).prop( "checked", false );
document.getElementById("tdCoin").innerHTML = loader;
document.getElementById("tr2td7").innerHTML = "";
// scoring
// four in a row
if (ranA==1 && ranB==1 && ranC==1 && ranD==1) {handA=100; blackC=1};
if (ranA==2 && ranB==2 && ranC==2 && ranD==2) {handA=100; blackC=1};
if (ranA==3 && ranB==3 && ranC==3 && ranD==3) {handA=100; blackC=1};
if (ranA==4 && ranB==4 && ranC==4 && ranD==4) {handA=100; blackC=1};
if (ranA==5 && ranB==5 && ranC==5 && ranD==5) {handA=100; blackC=1};
if (ranA==6 && ranB==6 && ranC==6 && ranD==6) {handA=100; blackC=1};
//three in a row
//1
if (ranA==1 && ranB==1 && ranC==1) {handA=50; purpleC=1};
if (ranB==1 && ranC==1 && ranD==1) {handA=50; purpleC=1};
//2
if (ranA==2 && ranB==2 && ranC==2) {handA=50; purpleC=1};
if (ranB==2 && ranC==2 && ranD==2) {handA=50; purpleC=1};
//3
if (ranA==3 && ranB==3 && ranC==3) {handA=50; purpleC=1};
if (ranB==3 && ranC==3 && ranD==3) {handA=50; purpleC=1};
//4
if (ranA==4 && ranB==4 && ranC==4) {handA=50; purpleC=1};
if (ranB==4 && ranC==4 && ranD==4) {handA=50; purpleC=1};
//5
if (ranA==5 && ranB==5 && ranC==5) {handA=50; purpleC=1};
if (ranB==5 && ranC==5 && ranD==5) {handA=50; purpleC=1};
//6
if (ranA==6 && ranB==6 && ranC==6) {handA=50; purpleC=1};
if (ranB==6 && ranC==6 && ranD==6) {handA=50; purpleC=1};
//straights
if (ranA==1 && ranB==2 && ranC==3 && ranD==4) {handA=20; redC=1};
if (ranA==2 && ranB==3 && ranC==4 && ranD==5) {handA=20; redC=1};
if (ranA==3 && ranB==4 && ranC==5 && ranD==6) {handA=20; redC=1};
if (ranA==6 && ranB==5 && ranC==4 && ranD==3) {handA=20; redC=1};
if (ranA==5 && ranB==4 && ranC==3 && ranD==2) {handA=20; redC=1};
if (ranA==4 && ranB==3 && ranC==2 && ranD==1) {handA=20; redC=1};
//pairs
//1
if (ranA==1 && ranB==1) {handA=5; greenC=1};
if (ranA==1 && ranC==1) {handA=5; greenC=1};
if (ranA==1 && ranD==1) {handA=5; greenC=1};
if (ranB==1 && ranC==1) {handA=5; greenC=1};
if (ranB==1 && ranD==1) {handA=5; greenC=1};
if (ranC==1 && ranD==1) {handA=5; greenC=1};
//2
if (ranA==2 && ranB==2) {handA=5; greenC=1};
if (ranA==2 && ranC==2) {handA=5; greenC=1};
if (ranA==2 && ranD==2) {handA=5; greenC=1};
if (ranB==2 && ranC==2) {handA=5; greenC=1};
if (ranB==2 && ranD==2) {handA=5; greenC=1};
if (ranC==2 && ranD==2) {handA=5; greenC=1};
//3
if (ranA==3 && ranB==3) {handA=5; greenC=1};
if (ranA==3 && ranC==3) {handA=5; greenC=1};
if (ranA==3 && ranD==3) {handA=5; greenC=1};
if (ranB==3 && ranC==3) {handA=5; greenC=1};
if (ranB==3 && ranD==3) {handA=5; greenC=1};
if (ranC==3 && ranD==3) {handA=5; greenC=1};
//4
if (ranA==4 && ranB==4) {handA=5; greenC=1};
if (ranA==4 && ranC==4) {handA=5; greenC=1};
if (ranA==4 && ranD==4) {handA=5; greenC=1};
if (ranB==4 && ranC==4) {handA=5; greenC=1};
if (ranB==4 && ranD==4) {handA=5; greenC=1};
if (ranC==4 && ranD==4) {handA=5; greenC=1};
//5
if (ranA==5 && ranB==5) {handA=5; greenC=1};
if (ranA==5 && ranC==5) {handA=5; greenC=1};
if (ranA==5 && ranD==5) {handA=5; greenC=1};
if (ranB==5 && ranC==5) {handA=5; greenC=1};
if (ranB==5 && ranD==5) {handA=5; greenC=1};
if (ranC==5 && ranD==5) {handA=5; greenC=1};
//6
if (ranA==6 && ranB==6) {handA=5; greenC=1};
if (ranA==6 && ranC==6) {handA=5; greenC=1};
if (ranA==6 && ranD==6) {handA=5; greenC=1};
if (ranB==6 && ranC==6) {handA=5; greenC=1};
if (ranB==6 && ranD==6) {handA=5; greenC=1};
if (ranC==6 && ranD==6) {handA=5; greenC=1};
}
function randomCoin() {
var handB = handA;
var greenCB = greenC;
var redCB = redC;
var purpleCB = purpleC;
var blackCB = blackC;
alert(" hand "+handB+" green "+greenCB+" red "+redCB+" purple "+purpleCB+" black "+blackCB);
var random = Math.floor((Math.random() * 2) + 1);
// coin display
if (random==1){document.getElementById("tdCoin").innerHTML = coin1;};
if (random==2){document.getElementById("tdCoin").innerHTML = coin2;};
// heads true
if (random==1 && $('#checkHeads').is(':checked')){
document.getElementById("tr2td7").innerHTML = "<h2>Win!</h2>";
var handC = handB * 2;
var greenCC = greenCB * 2;
var redCC = redCB * 2;
var purpleCC = purpleCB * 2;
var blackCC = blackCB * 2;
}
// tails true
if (random==2 && $('#checkTails').is(':checked')){
document.getElementById("tr2td7").innerHTML = "<h2>Win!</h2>";
var handC = handB * 2;
var greenCC = greenCB * 2;
var redCC = redCB * 2;
var purpleCC = purpleCB * 2;
var blackCC = blackCB * 2;
}
// heads false
if (random==1 && $('#checkTails').is(':checked')){
document.getElementById("tr2td7").innerHTML = "<h2>False.</h2>";
var handC = handB * 0;
var greenCC = greenCB * 0;
var redCC = redCB * 0;
var purpleCC = purpleCB * 0;
var blackCC = blackCB * 0;
}
// tails false
if (random==2 && $('#checkHeads').is(':checked')){
document.getElementById("tr2td7").innerHTML = "<h2>False.</h2>";
var handC = handB * 0;
var greenCC = greenCB * 0;
var redCC = redCB * 0;
var purpleCC = purpleCB * 0;
var blackCC = blackCB * 0;
}
// skip
if ($('#checkSkip').is(':checked')){
{document.getElementById("tdCoin").innerHTML = loader;};
var handC = handB * 1;
var greenCC = greenCB * 1;
var redCC = redCB * 1;
var purpleCC = purpleCB * 1;
var blackCC = blackCB * 1;
}
alert(" hand "+handB+" green "+greenCB+" red "+redCB+" purple "+purpleCB+" black "+blackCB);
alert(" hand "+handC+" green "+greenCC+" red "+redCC+" purple "+purpleCC+" black "+blackCC);
var totalHand = (totalHand + handC);
var greenChipTotal = (greenChipTotal + greenCC);
var redChipTotal = (redChipTotal + redCC);
var purpleChipTotal = (purpleChipTotal + purpleCC);
var blackChipTotal = (blackChipTotal + blackCC);
document.getElementById("gameTotal").innerHTML = "Total: " + totalHand;
document.getElementById("gameHand").innerHTML = "Hand: " + handC;
document.getElementById("chip5").innerHTML = greenChipTotal;
document.getElementById("chip20").innerHTML = redChipTotal;
document.getElementById("chip50").innerHTML = purpleChipTotal;
document.getElementById("chip100").innerHTML = blackChipTotal;
if (totalHand.value == 100) {alert("Winner Winner Chick Dinner!!!");};
}
function resetGame(){
// sets dice to loader
document.getElementById("tdDiceA").innerHTML = loader;
document.getElementById("tdDiceB").innerHTML = loader;
document.getElementById("tdDiceC").innerHTML = loader;
document.getElementById("tdDiceD").innerHTML = loader;
//resets checkboxes, coin picture and saying
$( "#checkHeads" ).prop( "checked", false );
$( "#checkTails" ).prop( "checked", false );
$( "#checkSkip" ).prop( "checked", false );
document.getElementById("tdCoin").innerHTML = loader;
document.getElementById("tr2td7").innerHTML = "";
//clears game info
document.getElementById("gameTotal").innerHTML = "";
document.getElementById("gameHand").innerHTML = "";
document.getElementById("chip5").innerHTML = "";
document.getElementById("chip20").innerHTML = "";
document.getElementById("chip50").innerHTML = "";
document.getElementById("chip100").innerHTML = "";
var totalHand = 0;
var handb = 0;
var greenChipTotal = 0;
var redChipTotal = 0;
var purpleChipTotal = 0;
var blackChipTotal = 0;
var handB = 0;
var greenCB = 0;
var redCB = 0;
var purpleCB = 0;
var blackCB = 0;
}
最佳答案
我相信这是因为在您尝试添加handC之前未初始化totalHand。由于未初始化,因此本质上为null。因此,您的语句显示为“将handC的值添加为null,并将该值存储在变量totalHand中”。您可能会猜到,不能将整数添加为null。
您应该可以通过将代码替换为以下内容来解决此问题。
var totalHand = 0;
将此添加到您的函数中。
totalHand += handC;
这会将totalHand设置为零,而不是null。
编辑:要保持总分,您将需要另一个变量。在计算totalHand之后将其添加。
var totalScore = 0;
将此添加到您的函数中。
totalScore += totalHand;