我的语法错误在哪里? JavaScript的

时间:2014-09-16 19:31:21

标签: javascript if-statement

var userChoice = function(answer) 
{
    prompt("Do you choose rock, paper, or scissors?");
}
var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice is between 0 and 0.33) {
    computerChoice = "rock";
} else if (computerChoice is between 0.34 and 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

我得到了

  

SyntaxError:意外的标识符

我的错误在哪里?

3 个答案:

答案 0 :(得分:1)

没有“介于”之间的操作员。您需要使用>=<=,如下所示:

var userChoice = function(answer) 
{
    prompt("Do you choose rock, paper, or scissors?");
}
var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice >= 0 && computerChoice <= 0.33) {
    computerChoice = "rock";
} else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

答案 1 :(得分:1)

如果您打算使用某种语言,请花一些时间查找语法。

没有&#39;是......和&#39;构造

您必须使用逻辑AND,就像一些评论所指出的那样:

if (computerChoice > 0 && computerChoice < 0.33)

答案 2 :(得分:1)

无需测试下限。

试试这个:

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

userChoice();
var computerChoice = Math.random();

if (computerChoice < 0.33) {
    computerChoice = "rock";
} else if (computerChoice < 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log(computerChoice);