7. What if Choice1 is paper? Token else


#1

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

I'm scratching my head at the issue I'm having with my code here. Everything looks fine but obviously I missed some direction. When I hit the button to submit, the console says that I have a token else.

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } 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";
        }
    }
}.

#2

It looks like you are missing a few things and a couple of indents are out of line.

this line should set choice1 equal to "rock"
also, this is how the format should be for the remainder of your code:
notice the indentions!!!

I hope this helps you, feel free to ask any other questions if you need more help understanding why anything is the way it is...


#3

Thanks for the feedback, but could you be more specific about what you mean on choice1 being "rock"? Also, the reason the indentation may be out of line is because I pasted my code into a JavaScript beautifier. Thanks a bunch!


#4

I tried to line mine up the way you did :confused: but it says "SyntaxError: Unexpected token else" i probably messed up a semi colon or something. They alwaysss confuse me. Any easy way to tell how many you need?

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 === "paper") {
return "scissors wins";
}
}
};


#5

It's more than likely an error with your indenting. Please post your formatted code and I will take a look.


#6

Hi, thanks. I actually ended up retyping the code, and it turns out I miss a closing bracket somewhere. But now when I'm having another issue. When I run the code, codecademy is telling me "Your code returned 'rock wins' instead of 'scissors wins' when the inputs are paper and scissors". Here's a look at my revised code.

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


#7


I took screenshots lol


#8

I am getting
Oops, try again. Did you set var computerChoice = Math.random() ? The capital 'M' in 'Math' is important!

var rock = "rock";
var paper = "paper";
var scissors = "scissors";

var userChoice = prompt("enter rock,paper or scissors");
var computerChoice = Math.random();
if (computerChoice < 0.33 ){
computerChoice= "rock";
}
else if (computerChoice < 0.67){
computerChoice= "paper";
}
else {
computerChoice= "scissors";
}
console.log( computerChoice );

var compare = function (player1,player2) {
if(player1 == player2 )
console.log("its a tie!");

else if(player1==rock)
{
if(player2==paper)
console.log(player2+" wins!");
if(player2==scissors)
console.log(player1 +"wins!");
}
else if(player1==paper)
{
if(player2==rock)
console.log(player1 +"wins!");
if(player2==scissors)
console.log(player2 +"wins!");
}
else if(player1==scissors)
{
if(player2==rock)
console.log(player2 +"wins!");
if(player2==paper)
console.log(player1 +"wins!");

}
};

compare(userChoice,computerChoice);


#9

@ cssrunner
your code is fixed. moved an else if statement down.

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

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

};