gpt4 book ai didi

Javascript ... 显示值

转载 作者:行者123 更新时间:2023-11-30 06:35:37 26 4
gpt4 key购买 nike

我正在用 Javascript 创建一个二十一点纸牌游戏。到目前为止,我很感激你的评论。我正在寻找关于为什么我似乎只能在 cardDealer 函数中调用一次 cardFace 函数的反馈。目前 cardFace(0) 给我一个值,而 cardFace(1) 或任何其他数字没有,除非它是 cardDealer 函数中唯一被调用的 cardFace(x) 。任何想法,将不胜感激。谢谢

下面的代码并不完整,但我相信足以从中辨别出足够的信息。谢谢

    function card(name, suit, face, value)
{
this.name = name;
this.suit = suit;
this.face = face;
this.value = value;
}

aceOfHearts = new card("aceofhearts","hearts", "ace", 11);
kingOfHearts = new card("kingofhearts","hearts", "king", 10);
queenOfHearts = new card("queenofhearts","hearts", "queen", 10);
jackOfHearts = new card("jackofhearts","hearts", "jack", 10);
tenOfHearts = new card("tenofhearts","hearts", "ten", 10);
nineOfHearts = new card("nineofhearts","hearts", "nine", 9);
eightOfHearts = new card("eightofhearts","hearts", "eight", 8);
sevenOfHearts = new card("sevenofhearts","hearts", "seven", 7);
sixOfHearts = new card("sixofhearts","hearts", "six", 6);


var deck;
deck = [];


deck.push(aceOfHearts);
deck.push(aceOfDiamonds);
deck.push(aceOfClubs);
deck.push(aceOfSpades);
deck.push(kingOfClubs);
deck.push(kingOfDiamonds);
deck.push(kingOfHearts);
deck.push(kingOfSpades);
deck.push(queenOfClubs);
deck.push(queenOfDiamonds);
deck.push(queenOfHearts);
deck.push(queenOfSpades);
deck.push(jackOfClubs);
deck.push(jackOfDiamonds);
deck.push(jackOfHearts);
deck.push(jackOfSpades);
deck.push(tenOfClubs);
deck.push(tenOfDiamonds);
deck.push(tenOfHearts);
deck.push(tenOfSpades);


var cardDealer = function()
{
fisherYates(deck);

document.getElementById("yourFirstCard").textContent = "Your First Card is " + cardFace(0) ;
document.getElementById("yourSecondCard").textContent = "Your Second Card is " + cardFace(1) ;

};

var cardFace = function(x)
{
cardFace = deck[x].face;
return cardFace;
};


var cardSuit = function(x)
{
cardSuit = deck[x].suit;
return cardSuit;
};

<body>
<div>Black Jack 1.1</div>
<br/>
<button type="button" onClick="cardDealer()">Deal</button>
<div id="yourFirstCard"></div>
<div id="yourSecondCard"></div>
<div id="yourThirdCard"></div>
<div id="yourFourthCard"></div>
<div id="yourFifthCard"></div>
<div id="playerTotal"></div></div>
<div id="playerSit"></div>
<div id="playerMessage"></div>
<div id="sitter"><button type="button" onClick="sit()">Sit</button></div>
<div id="hitter"><button type="button" onClick="hitMe()">Hit</button></div>
<br/>
<br/>
<div id="dealersFirstCard"></div></div>
<div id="dealersSecondCard"></div></div>
<div id="dealersThirdCard"></div></div>
<div id="dealersFourthCard"></div></div>
<div id="dealersFifthCard"></div></div>
<div id="dealerTotal"></div></div>
<div id="dealerSit"></div></div>
<div id="dealerMessage"></div></div>
</body>

最佳答案

这是我看到的问题:

var cardFace = function(x)
{
cardFace = deck[x].face;
return cardFace;
};

首先,您声明一个名为 cardFace 的变量并将其分配给一个函数。该函数实际执行的操作是重新分配您的 cardFace 变量并将其作为结果返回。就是这样,您的功能现在消失了,您不能再引用它,因为 cardFace 已被重新分配。

您需要做的是更改函数体,使其不会重新分配变量:

var cardFace = function(x)
{
var cardFaceValue = deck[x].face;
return cardFaceValue;
};

cardSuit 函数也是如此。

关于Javascript ... 显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744732/

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