What if Choice1 = Paper? (Error = Syntax Error: Unexpected token { )


#1

What is wrong with this code?

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 "paper wins";
}
}
else if (choice1 === "paper") {
if (choice2 === scissors) {
return "paper wins"
} else {
return "scissors wins"
}
};


#2

by switching

with

if (choice1 === "paper") {
if (choice2 === scissors) {
return "paper wins"
} else {
return "scissors wins"
}
}
};

you are writing a separate "if" statement for each choice that will be run when the previous "if" was not initialized. Each choice1 has its own "if" statement within the function that will run when their condition has been met. Otherwise, "else if choice1==="paper"" will never be executed because "if choice1==="rock" was never initialized. Hope that makes sense?


#3

btw, you needed that final curly bracket } to close the overall function as well


#4

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