4 Build "Rock, Paper, Scissors"


#1

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#

I'm getting an error message that says there is a problem with my syntax. In the preview box to the right I'm seeing text that says "Syntax error. Unexpected token else".
I'm supposed to write the last part of the compare function (if scissors is my first choice, and the second is rock, rock should win, else scissors should win).

Here is my code. I don't understand what's wrong with it, but I think it's somewhere in the brackets. I have no idea.

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

compare(userChoice, computerChoice);

#2

Wait, don't answer this, I found my mistake. Yes it was in the brackets.
However, I'm still getting an error even though in the preview box my code works as it should. The error is "Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper".
But this is what I see in the preview box: "Computer: rock "The result is a tie!" "
I don't understand. =D


#3

two problems:

else if (choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        } 
        else {
            return "scissors wins";
        }
/* missing } to close else if */

and:

else {
            (choice2 === "paper");
            return "scissors wins";
        }
/* oops, you can't do a compare in else, else is everything else, remove  (choice2 === "paper"); */

if you need more help, post an updated version of your full code


#4

2 posts were split to a new topic: 4. build rock paper scissors


#5

I'm having trouble with my code can you tell me what I'm doing wrong? I keep getting ("SyntaxError: Unexpected token else ")

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

#6

your code is a mess, you are suppose to make 3 else if statements after checking the result is a tie:

if (choices are equal){}
else if (choice1 is rock){}
else if (choice1 is paper){}
else if (choice1 is scissors){}

then inside each else if, check for choice2:

else if (choice1 is rock){
   if (choice2 is scissors){
    return rocks wins
  } else {
    return paper wins
  }
}

the above is pseudo code to give you an idea of the structure. Don't forget to to close the else if before moving on to the next one.