gpt4 book ai didi

javascript - 游戏评分返回 NaN,+= 运气不好

转载 作者:行者123 更新时间:2023-12-03 10:16:59 28 4
gpt4 key购买 nike

我将尽力在这里描述我的问题。我的 js 代码应该从 if 语句为 hand 和chip 变量分配一个值。然后将其传递给另一个函数,该函数根据硬币翻转对其应用乘数。最后,将结果写入页面,并在每次重置手牌时保留分数的总数。

我相信我的问题是当我尝试执行 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 尚未初始化。由于它没有初始化,所以它本质上是空的。因此,您的语句为“将 handC 的值添加为 null,并将该值存储在变量totalHand 中”。正如您可能猜到的,您不能将整数添加到 null。

您应该能够通过将代码替换为以下代码来解决此问题。

var totalHand = 0;

将其添加到您的函数中。

totalHand += handC;

这会将totalHand设置为零而不是null。

编辑:要保留总分,您将需要另一个变量。在计算totalHand后添加此内容。

var totalScore = 0;

将其添加到您的函数中。

totalScore += totalHand;

关于javascript - 游戏评分返回 NaN,+= 运气不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830254/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com