哪里出现意外的语法错误?

时间:2015-07-17 00:27:44

标签: javascript if-statement

控制台声明我的代码中有一个意外的{。我多次查看语法,似乎无法找到它。

var userChoice = prompt("Do you choose rock, paper, or scissors?");

var computerChoice = Math.random(1);

if (computerChoice <= 0.33){
    computerChoice = "rock";
} elseif (computerChoice <= 0.67 && computerChoice >= 0.34) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

3 个答案:

答案 0 :(得分:1)

它应该是else if(不在一起)

编辑:这是工作代码

var userChoice = prompt("Do you choose rock, paper, or scissors?");

var computerChoice = Math.random(1);

if (computerChoice <= 0.33){
    computerChoice = "rock";
} else if (computerChoice <= 0.67 && computerChoice >= 0.34) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

答案 1 :(得分:1)

elseif之间应该有空格。

var userChoice = prompt("Do you choose rock, paper, or scissors?");

var computerChoice = Math.random(1);

if (computerChoice <= 0.33){
    computerChoice = "rock";
} else if (computerChoice <= 0.67 && computerChoice >= 0.34) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

答案 2 :(得分:0)

问题在于您构成了关键字elseif,可能打算else if

为了将来参考,控制台还会告诉您错误所在的行;之后调查每一项技术(检查你的拼写,查找含义等)来发现你做错了什么,这相当容易。在这种情况下,非常快速的谷歌搜索&#34; javascript elseif&#34;得到你there