2014-09-22 105 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();