2016-11-30 87 views
0

((這是家庭作業的一部分,但我沒有要求任何人告訴我究竟寫什麼,所以掛在那裏我認爲這將很容易回答))爲什麼我不能編輯特定功能(javascript/html)以外的字段?

我有一個腳本模擬一個胡扯遊戲。我將在最後發佈它,但首先閱讀其餘的問題,因爲大多數代碼與我的問題無關。

我的工作是實施銀行和投注系統。聽起來很容易,但我卡住了。所以我的第一步是在結果表格中爲銀行寫一欄。接下來,我把這個代碼在play()函數:

bank = prompt("How much money is in your bank?"); 
document.getElementById("bankfield").value = bank; 

運行腳本,按下按鈕調用播放(),窗口彈出,輸入值,領域的變化。到現在爲止還挺好。但我意識到,我不想在那裏的代碼,因爲銀行不應該每次擲骰子時重置。所以我剪切並粘貼了兩行,直到腳本的開頭,因此它是運行時發生的第一件事。 ......現在我被提示輸入一個值之後,bankfield保持爲空,我也得到this error

爲什麼這兩條線工作得很好的發揮()函數,而不是公然在腳本?

我一直在搞這個很長一段時間,但我沒有取得進展我想我在這裏錯過了一個概念,並認爲這裏有人可以啓發我。謝謝!

這裏是refence原來的遊戲腳本:

 </style> 
    <script type = "text/javascript"> 

    // variables used to test the state of the game 
    var WON = 0; 
    var LOST = 1; 
    var CONTINUE_ROLLING = 2; 

    // other variables used in program 
    var firstRoll = true; // true if current roll is first 
    var sumOfDice = 0; // sum of the dice 
    var myPoint = 0; // point if no win/loss on first roll 
    var gameStatus = CONTINUE_ROLLING; // game not over yet 

    // process one roll of the dice 
    function play() 
    { 
     // get the point field on the page 
     var point = document.getElementById("pointfield"); 

     // get the status div on the page 
     var statusDiv = document.getElementById("status"); 
     if (firstRoll) // first roll of the dice 
     { 
      sumOfDice = rollDice(); 

      switch (sumOfDice) 
      { 
       case 7: case 11: // win on first roll 
       gameStatus = WON; 
       // clear point field 
       point.value = ""; 
       break; 
       case 2: case 3: case 12: // lose on first roll 
       gameStatus = LOST; 
       // clear point field 
       point.value = ""; 
       break; 
       default: // remember point 
       gameStatus = CONTINUE_ROLLING; 
       myPoint = sumOfDice; 
       point.value = myPoint; 
       firstRoll = false; 
      } // end switch 
     } // end if 
     else 
     { 
      sumOfDice = rollDice(); 

      if (sumOfDice == myPoint) // win by making point 
       gameStatus = WON; 
      else 
       if (sumOfDice == 7) // lose by rolling 7 
       gameStatus = LOST; 
     } // end else 

     if (gameStatus == CONTINUE_ROLLING) 
      statusDiv.innerHTML = "Roll again"; 
     else 
     { 
      if (gameStatus == WON) 
       statusDiv.innerHTML = "Player wins. " + 
       "Click Roll Dice to play again."; 
      else 
       statusDiv.innerHTML = "Player loses. " + 
       "Click Roll Dice to play again.";  

      firstRoll = true; 
     } // end else 
    } // end function play 

    // roll the dice 
    function rollDice() 
    { 
     var die1; 
     var die2; 
     var workSum; 

     die1 = Math.floor(1 + Math.random() * 6); 
     die2 = Math.floor(1 + Math.random() * 6); 
     workSum = die1 + die2; 

     document.getElementById("die1field").value = die1; 
     document.getElementById("die2field").value = die2; 
     document.getElementById("sumfield").value = workSum; 

     return workSum; 
    } // end function rollDice 
    // 
    </script> 

我很新的,一般JS和CS菜鳥,所以要溫柔。另外我的第一個問題在這裏被問到,如果我的格式不好或者其他問題很抱歉。

+0

將'