2014-09-22 108 views
0

我的自我教育進展緩慢,我遇到了問題。我試圖從用戶那裏獲得文本輸入。然後,我希望該輸入是針對自身的大寫版本進行檢查的。我通過labs.codecademy.com運行代碼(如果這是相關的)。string.toUpperCase困難

代碼:

function rollerInput() { 
    var input = prompt("Do you want to reroll? You have " + rerolls + " rerolls left. Y/N?"); 
    input = input.toUpperCase; 
    if(input === null) { 
     console.log("Please enter Y or N"); 
     rollerInput(); 
     } else if(input === "") { 
      console.log("Please enter Y or N"); 
      rollerInput(); 
     } else if(input === "Y" && rerolls !== 1) { 
      rerolls = rerolls -1; 
      reRoller(); 
     } else if(input === "Y" && rerolls === 1) { 
      console.log("You have no rerolls left."); 
     } 
} 

問題在於與檢查Y或N.無論ÿ或y產生任何反應的IFS。我確信我錯過了一些簡單的東西,但我無法修復它以挽救我的生命。

+1

這是一個方法,你必須用括號把它稱爲'()':'輸入= input.toUpperCase();' – vikingmaster 2014-09-22 21:00:55

+0

@GregHewgill&SterlingArcher的感謝!我的錯。 – 2014-09-22 22:16:42

回答

0

input = input.toUpperCase存儲功能toUpperCase在變量input調用它。你想調用的功能。

使用

input = input.toUpperCase(); 

這是x = function() { }x = (function() { })()之間的差 - 第一個存儲功能,第二個執行的函數和存儲的返回值。

另一個問題是,您在之前盲目地調用toUpperCase,您會檢查null。如果用戶取消對話框,則會導致錯誤。當您知道input.toUpperCase實際上將被定義時,您需要在您的null支票內移動您的測試。

+0

我感到格外尷尬。謝謝。 – Malaugrym 2014-09-22 21:29:27

0

toUpperCase是一個方法,你必須用括號()

input = input.toUpperCase(); 
0

是的,你沒有調用函數。

input = input.toUpperCase();