我试图让当你点击一个按钮时,它会显示一副纸牌。我为每种花色都有一个按钮,所以当你点击它时,它就会处理该花色。我已经用它来处理红心,但当我尝试处理钻石时,它仍然显示红心。我究竟做错了什么?我该如何展示我的钻石?var heart = true;var diamond = true;var suit = "";function card(name,suit,value) { this.name = name; this.suit = suit; this.value = value;}var deck = [ new card('Ace', 'Hearts',11), new card('Two', 'Hearts',2), new card('Three', 'Hearts',3), new card('Four', 'Hearts',4), new card('Five', 'Hearts',5), new card('Six', 'Hearts',6), new card('Seven', 'Hearts',7), new card('Eight', 'Hearts',8), new card('Nine', 'Hearts',9), new card('Ten', 'Hearts',10), new card('Jack', 'Hearts',10), new card('Queen', 'Hearts',10), new card('King', 'Hearts',10), new card('Ace', 'Diamonds',11), new card('Two', 'Diamonds',2), new card('Three', 'Diamonds',3), new card('Four', 'Diamonds',4), new card('Five', 'Diamonds',5), new card('Six', 'Diamonds',6), new card('Seven', 'Diamonds',7), new card('Eight', 'Diamonds',8), new card('Nine', 'Diamonds',9), new card('Ten', 'Diamonds',10), new card('Jack', 'Diamonds',10), new card('Queen', 'Diamonds',10), new card('King', 'Diamonds',10), new card('Ace', 'Clubs',11), new card('Two', 'Clubs',2), new card('Three', 'Clubs',3), new card('Four', 'Clubs',4), new card('Five', 'Clubs',5), new card('Six', 'Clubs',6), new card('Seven', 'Clubs',7), new card('Eight', 'Clubs',8), new card('Nine', 'Clubs',9), new card('Ten', 'Clubs',10), new card('Jack', 'Clubs',10), new card('Queen', 'Clubs',10),];
1 回答
桃花长相依
TA贡献1860条经验 获得超8个赞
如果您不想为了更优雅的解决方案而重写所有内容,您可以在处理钻石时将牌组的索引添加 12。但是,如果您最终洗牌,则此方法不会起作用。
function dealDiamonds(d) {
if (d < 13) {
displayDiamonds = deck[d + 12];
//...
} else {
//...
}
}
一个更优雅的解决方案是拥有一个函数来获取某套特定的牌,而不依赖于牌组的特定顺序。
function dealDiamonds(d) {
if (d < 13) {
displayDiamonds = getCardOfSuit('Diamonds', d);
//...
} else {
//...
}
}
function getCardOfSuit(suit, index) {
//filter the deck to only the deck of a certain suit
let suitDeck = deck.filter((card) => card.suit == suit);
//returns the card (0 - 12) based on the second parameter
return suitDeck[index];
}
添加回答
举报
0/150
提交
取消