开发者

What is causing this script to fail or fall in to a infinite loop?

开发者 https://www.devze.com 2023-02-14 01:04 出处:网络
Why does it seem like my page goes in to a infinite loop when the dealHands() function is called? What should happen is, it should call the fetchCard() function. That function should create a random n

Why does it seem like my page goes in to a infinite loop when the dealHands() function is called? What should happen is, it should call the fetchCard() function. That function should create a random number between 0-51, then search the discardPile array to make sure that the random number doesn't existed within the discardPile array. If it doesn't then the fetchCard() function should return the random number back to the dealHands() function s开发者_如何转开发o that it can be assigned/added to the allHands array.

    //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;
    }


you have i++ in your loop, not x++

       for (x=0;x<=1;i++) {


for (x=0;x<=1;**i**++)

should be x.

0

精彩评论

暂无评论...
验证码 换一张
取 消