我学习了一些javascript并且遇到了一个绊脚石。我想从我的数组中选择2个随机值并将它们保存到一个名为Deal的变量(我猜这将是另一个数组)中,我可以获得一个随机值但不是2 ..我的代码如下
var deckOfCards = []; //declaring an empty array to put Cards into later
function Card(name, value, altValue) { //function that defines a card object ( key part is "this")
this.cardName = name;
this.cardValue = value;
this.cardAltValue = altValue || false;
}
var suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs'];
var values = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King'];
for(var i in suits) //loops through each of the suits
{
for(var j in values) { // loops through each of the cards for the suit
if(values[j] != 'Ace') {//j is the index within the array,
var cardValue = parseInt(j, 10) + 1;
if(cardValue > 10) cardValue = 10;
deckOfCards.push(new Card(values[j] +' of ' +suits[i], cardValue));//adding a new card object to deckOfCards
} else {
deckOfCards.push(new Card(values[j] +' of ' +suits[i], 1, 11));// adding alternate value to deckOfCards
}
}
}
var Deal = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];;
console.log(Deal);
任何人都可以给我一个指针吗?
谢谢