该代码应该创建一个函数,通过添加手中所有卡的值来计算并返回卡的分数。应从52个数组元素中随机选择八张或更少的纸牌。但是,Jack,Queen或King的值为10。
我已经能够遍历卡片并随机选择8张卡片。但是,我无法用每次出现的10替换Jack,King和Queen的值。
function showHand(){
var suits = ['spades', 'hearts', 'diamonds', 'clubs'];
var cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'];
var deal = [];
for(suit in suits){
for(card in cards){
deal.push(cards[card]);
}
}
var handCard = deal.splice(Math.floor(Math.random() * deal.length), 8);
var getGolfScore = golfScore(handCard);
document.getElementById('dvShowHand').innerHTML = list + '<br>' + getGolfScore;
}
function golfScore(hand){
var sumCardInHand = 0;
for(hands in hand){
if(hand[hands] == 'Jack' || hand[hands] == 'King' || hand[hands] == 'Queen'){
hand[hands] = 10;
}
sumCardInHand += hand[hands];
}
return sumCardInHand;
}
例如,如果手是此数组:[5,2,“ King”,3,1,“ Queen”],则 golfScore函数将返回31。