Unexpected Else Statement on What if choice1 is scissors?


#1

I've looked through the code, but it's not working!
/*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";
}
}
};
var compare = function(choice1, choice2) {
else if (choice1 == "paper") {
if (choice2 == "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
};


#2

That is weird, maybe check your spacing in between the lines or contact one of the moderators.

Here is one that helped me, have him check yours out:

@lolman


#3

@bgguan, Hello! So I see you've got a similar problem that others have had, so let's see if I can't help you figure it out!

When you're creating a function, you're assigning it to a variable correct? With that being said you can't "make" a variable more than once:
i.e.

var example = "this"
//And
var example = "that"

You can't do that in JavaScript, so you'll just have to make the compare function once, and get rid of the second compare function.

However, with that being said keep in mind that your opening and closing brackets will be off a little bit. You have all of the code that's suppose to be there, but the only things you need to fix are your closing brackets " } " and your second compare function needs to be deleted.

else {
 return "paper wins";
 }
 } 
}; //Delete this closing bracket!
var compare = function(choice1, choice2) { //Delete this function.
 else if (choice1 == "paper") {
 if (choice2 == "rock") {
 return "paper wins";
 }
 else {
 return "scissors wins";
 }
}
};

I believe after you make those changes that your code will be working and ready to go!

If you have any other questions or want me to explain anything in depth please just let me know and I'll do so! Have a great day!


#4

Thanks! I'll try that.