我刚刚开始学习JavaScript,并决定用我迄今所获得的有限知识制作一款石纸剪刀游戏。但是,我不知道它为什么输出。
I chose function (userChoice)
{
if(userChoice==="Rock")
return "Paper";
if(userChoice==="Paper")
return "Scissors";
if(userChoice==="Scissors")
return "Rock";
}. I win noob.而不仅仅是“我选择了摇滚我赢了诺布”
这是我的密码:
var main = function()
{
var yesno = confirm("Would you like to play Rock-Paper-Scissors?");
if(yesno === false)
return "You're lame.";
var userChoice = prompt("Rock, Paper, or Scissors?");
var cheat = function(userChoice)
{
if(userChoice==="Rock")
return "Paper";
if(userChoice==="Paper")
return "Scissors";
if(userChoice==="Scissors")
return "Rock";
};
return "I chose " + cheat + ". I win noob.";
};
console.log(main());我发现它的工作方式是将cheat函数移到外部和main函数之上,并调整一些东西,代码按预期工作。我只是不明白为什么一种方法起作用,而另一种则不起作用。
发布于 2014-01-16 16:31:42
你忘了()
return "I chose " + cheat(userChoice) + ". I win noob.";使用括号,JavaScript将在字符串中插入函数,而不是调用它和插入结果。
https://stackoverflow.com/questions/21167389
复制相似问题