2014-04-30 53 views
0

一些變量問題,我學到的JavaScript編程,這樣我可以添加一些功能到我的網站。我正在嘗試在瀏覽器中製作一個簡單的二十一點遊戲,現在我不知道發生了什麼。固定在JS和JQ

我有2個函數:第一個函數randomCard()生成一個隨機卡,第二個函數deal()使用for循環使用第一個函數兩次,並將卡存儲到一個數組中。我寧願不把這些功能結合起來,因爲未來還有其他地方會將它們分開來更方便。我用方法手在兩個對象,用戶和經銷商上使用交易功能。

現在,這裏的問題是,當我記錄這些值到控制檯(不是終點,只是爲了測試),它們是相同的每一次,我不知道爲什麼。但是當我只記錄deal()函數時,它們都是不同的(參見腳本的底部)。

我不出去,將不勝感激幫助,這裏是我的代碼至今。

我使用jQuery的1.11.0

<!DOCTYPE html> 

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Blackjack Game</title> 
    </head> 
    <body> 

     <button type="submit" id="deal">Deal</button> 
     <p class="user"> 
      Your cards: 
     </p> 
     <p class="dealer"> 
      The dealers cards: 
     </p> 

     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
     <script> 

     $(document).ready(function() { 

      // gives the user and dealer a DOM element 
      var $user_cards = $('.user'); 
      var $dealer_cards = $('.dealer'); 

      // hides the DOM elements until the game has started 
      $($user_cards).hide(); 
      $($dealer_cards).hide(); 

      // jQuery var for DOM element to start the game 
      var $deal = $('#deal'); 

      // var to track if the deal button has been clicked 
      var click = 0; 

      function randomCard() { 
       // random number between 0 and 10 
       var j = Math.random() * 10; 
       // round that number into a var called card 
       var card = Math.round(j); 
       // if card is 0, assign a J Q or K by making a random number again 
       if (card === 0) { 
        //another random number 
        var k = Math.random() * 10; 
        // checks random number and assign J Q or K      
        if (k <= 4) { 
         card = 'J'; 
        } else if (k <= 7) { 
         card = 'Q'; 
        } 
        else { 
         card = 'K'; 
        } 
       } 
       // value of the function is a single card 
       return card; 
      } 

      // empty array to store cards 
      var cards = []; 

      function deal() { 
       // var to start for loop 
       var i = 0;    
       // start for loop 
       for (i; i < 2; i++) { 
        // add a random card to the i^th index of cards 
        cards[i] = randomCard(); 
       } 
       // value fo function is array of two cards 
       return cards; 
      } 

      $($deal).click(function() { 

       // make the game start check = 1 
       click++; 
       // check to see if the game started 
       if (click === 1) { 
        // make the button disappear when game has started 
        $($deal).fadeToggle('fast'); 

        // makes the player DOM elements appear when game is started 
        $($user_cards).fadeToggle(2000); 
        $($dealer_cards).fadeToggle(2000); 
       } 

       // make an object for the user and store hand under user.hand 
       var user = { 
        hand: deal() 
       }; 

       // make an object for the dealer and store hand under dealer.hand 
       var dealer = { 
        hand: deal() 
       }; 

       console.log(user.hand + " " + dealer.hand); 
       console.log(deal()); 
       console.log(deal()); 
       console.log(deal()); 
       console.log(deal()); 
       console.log(deal()); 
       console.log(deal()); 


      }); // $deal.click() end 

     }); // document.ready() end 

     </script> 
    </body> 
</html> 
+0

僅供參考,沒有理由做'$($ user_cards).hide();'。你可以做'$ user_cards.hide();'。 '.hide()'是一個jQuery對象的方法,'$ user_cards'已經是一個jQuery對象,所以你可以直接使用它。無需通過執行'$($ user_cards)'來創建另一個jQuery對象。 – jfriend00

回答

0

好消息和一個壞消息:

好消息

你的代碼實際工作。

如果更換

console.log(deal()); 

console.log(JSON.stringify(deal())); 

,那麼你會得到你所期望的結果。

壞消息

的Chrome瀏覽器控制檯是「活」的 - 這意味着它顯示了一個對象的當前值 - 不是你登錄它的時間價值。

(如果你問我,我會打電話給這個錯誤 - 但我的票不計)

你有兩個解決方案。

解決方案1 ​​

使用console.logstringify爲我做了。

解決方案2

製作deal通過移動重新安排你的代碼略微返回一個新的和不同的對象。

function deal() { 
    // MOVE THESE LINES INSIDE THE FUNCTION. 
    // empty array to store cards 
    var cards = []; 

    // var to start for loop 
    var i = 0;    
    // start for loop 
    for (i; i < 2; i++) { 
    // add a random card to the i^th index of cards 
    cards[i] = randomCard(); 
    } 
    // value fo function is array of two cards 
    return cards; 
} 
0

也許這可以幫助你

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters

定義getter和setter

吸氣是獲取特定屬性的值的方法。 setter是一種設置特定屬性值的方法。您可以在支持添加新屬性的任何預定義核心對象或用戶定義對象上定義getter和setter。定義getter和setter的語法使用對象字面值語法。

應用到你的代碼:

<!DOCTYPE html> 

<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Blackjack Game</title> 
</head> 
<body> 

    <button type="submit" id="deal">Deal</button> 
    <p class="user"> 
     Your cards: 
    </p> 
    <p class="dealer"> 
     The dealers cards: 
    </p> 

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
    <script> 

    $(document).ready(function() { 

     // gives the user and dealer a DOM element 
     var $user_cards = $('.user'); 
     var $dealer_cards = $('.dealer'); 

     // hides the DOM elements until the game has started 
     $($user_cards).hide(); 
     $($dealer_cards).hide(); 

     // jQuery var for DOM element to start the game 
     var $deal = $('#deal'); 

     // var to track if the deal button has been clicked 
     var click = 0; 

     function randomCard() { 
      // random number between 0 and 10 
      var j = Math.random() * 10; 
      // round that number into a var called card 
      var card = Math.round(j); 
      // if card is 0, assign a J Q or K by making a random number again 
      if (card === 0) { 
       //another random number 
       var k = Math.random() * 10; 
       // checks random number and assign J Q or K      
       if (k <= 4) { 
        card = 'J'; 
       } else if (k <= 7) { 
        card = 'Q'; 
       } 
       else { 
        card = 'K'; 
       } 
      } 
      // value of the function is a single card 
      return card; 
     } 

     // empty array to store cards 
     var cards = []; 

     function deal() { 
      // var to start for loop 
      var i = 0;    
      // start for loop 
      for (i; i < 2; i++) { 
       // add a random card to the i^th index of cards 
       cards[i] = randomCard(); 
      } 
      // value fo function is array of two cards 
      return cards; 
     } 

     $($deal).click(function() { 

      // make the game start check = 1 
      click++; 
      // check to see if the game started 
      if (click === 1) { 
       // make the button disappear when game has started 
       $($deal).fadeToggle('fast'); 

       // makes the player DOM elements appear when game is started 
       $($user_cards).fadeToggle(2000); 
       $($dealer_cards).fadeToggle(2000); 
      } 

      // make an object for the user and store hand under user.hand 
      var user = { 
       //hand: function() { return deal() } 
       get hand() { return deal(); }, 
      }; 

      // make an object for the dealer and store hand under dealer.hand 
      var dealer = { 
       //hand: function() { return deal() } 
       get hand() { return deal(); }, 
      }; 

      console.log(user.hand + " " + dealer.hand); 
      console.log(deal()); 
      console.log(deal()); 
      console.log(deal()); 
      console.log(deal()); 
      console.log(deal()); 
      console.log(deal()); 


     }); // $deal.click() end 

    }); // document.ready() end 

    </script> 
</body>