What is the error of this code?


#1


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2) {
if (choice1 === choice2); {
    return "The result is a tie";
    }
}
compare(userChoice,computerChoice)


#2

The semi-colon is the problem. Remove it and your code looks good. Check the string in the return statement to be sure it is identical to the one in the instructions.


#3

Hi mtf, yes that was it. Small errors are important to catch. Thank you.


#4

So it seems that the code for the 2nd else if should be as the 1st else if, but with different parameters. However, it returns a syntax error: Expected token else. Testing the code before adding the 2nd else if it works. As soon as you add the required code for the 2nd else if, the syntax error appears. Is there something I've missed?

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock"){
if(choice2 === "scissors")
return "rock wins";
}else {
return "papers wins";
}
else if(choice1 === "paper"){
if(choice2 === "rock")
return "paper wins"
}else {
return "scissors win";
}
};
compare(userChoice,computerChoice)


#5

Check the inner if for missing opening braces, in both instances.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.