What if choice1 is rock?


#1

What if choice1 is "rock"? Given choice1 is "rock",

a. if choice2 === "scissors", then "rock" wins.
b. if choice2 === "paper", then "paper" wins.

< ALL error messages.>
SyntaxError: Unexpected keyword 'else' It says my problem is in else if.

/*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";
    }     
}

Please Help. What am I doing wrong.

I looked at the Hint as well, and my code is the same as theirs. see beloe --

if(choice1 === choice2) {
    return "The result is a tie";
}

else if(choice1 === "rock") {

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

}

#2

A couple things going on here. Firstly you need a opening after you if statement. You also don't need to use parenthesis in a return statement. Also not supposed to put a semi colon after your if statement. Making it like this.

if (choice1 === choice2) {
    return "The result is a tie!";
}

Then add a closing bracket right at the end of you text.

All of this.

Needs to be inside this function.


#3

okay, I think I've fixed the Syntax error now.

However I still get an error Oops, try again. Your code returned 'paper wins' instead of 'rock wins' when the inputs are rock and scissors

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";
    }
 }
}

#4

I think this has to be all lower case.

And missing a semi colon here.


#5

You're brilliant! thank you. It was the Caps Lock on Scissors that caused the issue.


#7

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