Reputation: 11
I'm new, so forgive my question if its somewhat banal. I received this error while learning Javascript: SyntaxError: Parser error
Here's my code(I'm writing a simple 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);
Where did I go wrong?
Upvotes: 0
Views: 47
Reputation: 7775
You forgot to declare computerChoice
as a variable:
var computerChoice = null; //watch out "var"
Upvotes: 0
Reputation: 5808
Give function name first with parenthesis , then always use syntax of if else which automatically create parenthesis pair, work in it.....so never you get error.
To find this type of error use firebug addons of firefox.. which tell you exact line no...(if single js you called else it give the appropriate code with line no.)
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);
Upvotes: 1