分析器错误(Javascript初学者寻找解决方案)

时间:2015-01-21 06:34:44

标签: javascript

我是新人,请原谅我的问题,如果它有点平庸。 我在学习Javascript时收到此错误: SyntaxError:分析器错误

这是我的代码(我正在编写一个简单的Rock,Paper,Scissors Program):

var userChoice = prompt("Rock, Paper or Scissors?");
computerChoice = "null";

var randomnumber = Math.floor(Math.random()*1);
if (randomnumber <= (1/3)){
        computerChoice = "Rock";
    }
else if ((randomnumber > (1/3)) && (randomnumber <= (2/3))){
        computerChoice = "Paper";
    }
else {
    computerChoice = "Scissors";
}    
}
console.log(computerChoice);

我哪里出错了?

2 个答案:

答案 0 :(得分:1)

首先用括号给出函数名,然后总是使用if else的语法自动创建括号对,在其中工作.....所以永远不会得到错误。

要找到这种类型的错误,请使用firefox的firebug插件..它告诉你确切的行号...(如果单个js你调用else,它会给出带有行号的相应代码。)

var userChoice = prompt("Rock, Paper or Scissors?");
computerChoice = "null";

var randomnumber = Math.floor(Math.random()*1);
if (randomnumber <= (1/3))
{
        computerChoice = "Rock";
}
else if ( (randomnumber > (1/3)) && (randomnumber <= (2/3)) )
{
        computerChoice = "Paper";
}
else {
    computerChoice = "Scissors";
}    
//} this is wrong.
console.log(computerChoice);

答案 1 :(得分:0)

您忘了将computerChoice声明为变量:

var computerChoice = null; //watch out "var"