dealHands()
関数が呼び出されたとき、私のページが無限ループに入るのはなぜですか?何が起こるはずです、それはfetchCard()
関数を呼び出す必要があります。その関数は0-51の間の乱数を作成し、discardPile
配列内で乱数が存在しないことを確認するために配列discardPile
を検索します。そうでなければ、fetchCard()
関数はdealHands()
関数に乱数を返して、allHands
配列に代入/追加できるようにする必要があります。このスクリプトが失敗するか、無限ループに入る原因は何ですか?
//Variables
var discardPile = new Array();
var allHands = new Array();
//Prototypes
Array.prototype.exists = function(search) {
for(i=0;i<this.length;i++)
if (this[i] == search) return true;
return false;
}
//Functions (Only the ones the that are needed for this question)
function dealHands() {
var cardsOfHand = new Array()
for (x=0;x<=1;i++) {
for (y=0;y<=1;y++) {
cardsOfHand[y] = fetchCard();
discardCard(cardsOfHand[y]);
}
allHands[x] = cardsOfHand
}
}
function discardCard(card) {
var totalCards = discardPile.length;
if (totalCard != 0) { totalCards++ }
discardPile[totalCards] = card;
}
function fetchCard() {
var usedCard = true;
while(usedCard == true) {
var randomCard = Math.floor(Math.random()*51);
usedCard = discardPile.exists(randomCard);
}
return randomCard;
}
ありがとうございます。とても小さい。 – SgtOJ
それはプログラミングの喜びです。 –