ROCK, PAPER, SCISSORS, PART 8 (Where's the bug?)

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Hey everyone, I’m stuck on Part 8 of the Rock, Paper, Scissors exercise. I’ve been looking at other posts about it, but I can’t seem to figure out what’s wrong.

<In what way does your code behave incorrectly? Include ALL error messages.>
I’m getting “SyntaxError: Unexpected token {”, but I can’t figure out where it is. I have everything the same as I had it in the last part, which was successful. I’m hoping fresh eyes will be able to find the error. Thanks for your help!

```

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

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

};

console.log(compare(userChoice, computerChoice));

<do not remove the three backticks above>

HI the else statement doesn’t take any condition this part…

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

Hey, thanks for your help. I’m not sure what you mean though. Are you saying it should take a condition and I haven’t included it, or that it shouldn’t and I have? Would you mind showing me what it should look like? Thanks.

If you put your code in Codecademy Labs you can see which line of your code got a problem. It’s a useful tool.

1 Like

the if else if and else statement

if (condition) {

}
else if (condition) {

}
else {

}

Do you see now what I mean?

Okay, yes I do. And it worked! Thanks a lot for your help.

1 Like

Thanks for the tip, I’ll be using that in the future for sure!

Hello all,

I completed the program and based on the codecademy standards, my code is right. But when I try to run this code in another compiler, like JSFiddle, it wont run after the prompt entry. Any ideas what could be wrong? Here is my 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);

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

Any assistance is this matter would be greatly appreciated!

:v:

-Mike

You did not call the function compare with arguments/actual parameters/inputs.
compare(userChoice,computerChoice); If you do not call the function it will not work.

It will give you output through return. But if you want to display the output you can use console.log();

Thank you for your reply!

So you’re saying that choice 1 and choice2 are just place holders? And so I have to replace them with userChoice and computerChoice?

I made some changes based on your advice. Now the restult is always a tie:

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);

function compare(userChoice, computerChoice){
    if(userChoice === computerChoice){
        alert("The result is a tie!");
    }
    else if(computerChoice === "scissors"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("paper wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "scissors"){
            alert("scissor wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("scissors wins!");
            }
        }
}
compare();

You can say they sort of placeholder but actually I’m not sure if it’s a correct term. You should better say those parameters. and the inputs as arguments. Although arguments sometimes referred as parameters too.

Okay, that makes more sense! Well I replaced the parameters as you advised but now its returning the result as a tie. Any idea what else i’m doing wrong? My updated code is above

It’s likely to be a tie as your code:[quote=“mcordero1206, post:13, topic:39955”]
else if(computerChoice === “scissors”){
alert(“rock wins!”);
if(computerChoice === “paper”){
alert(“paper wins!”);
}
}
[/quote]

In this case, from there and the rest, you are considering only one input/argument computerChoice. Only in tie, two arguments are evaluated.
Hmm, then arguments sound better as a name for them. You need to change those bits of code to something like this:

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

I noticed you changed your code quite a bit. You do not need to change at the top(choice1,choice2) and the whole body of the code from choice 1/choice 2 to userChoice/computerChoice. You can just call the function at the end with your first set of code. Like this:
compare(userChoice, computerChoice);
But both cases your first sets of code and second sets of code you need to consider both of the choices to avoid a tie.
But again check and get feedback from others too.

I’m not sure what you meant by this?[quote=“mashuk, post:16, topic:39955”]
I noticed you changed your code quite a bit. You do not need to change at the top(choice1,choice2) and the whole body of the code from choice 1/choice 2 to userChoice/computerChoice. You can just call the function at the end with your first set of code. Like this:compare(userChoice, computerChoice);But both cases your first sets of code and second sets of code you need to consider both of the choices to avoid a tie.But again check and get feedback from others too.
[/quote]

You could update your first code like this:

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);

function compare(choice1, choice2){
if(choice1 === choice2){
return “The result is a tie!”;
}
else if(choice2 === “scissors”){
return “rock wins!”;
if(choice2 === “paper”){
return “paper wins!”;
}
}
else if(choice2 === “rock”){
return “rock wins!”;
if(choice2 === “scissors”){
return “scissor wins!”;
}
}
else if(choice2 === “rock”){
return “rock wins!”;
if(choice2 === “paper”){
return “scissors wins!”;
}
}
}
compare(userChoice,computerChoice);

And you can use your choice of display method to display the output.
alert(compare(userChoice,computerChoice)); or console.log((userChoice,computerChoice)); By the way with this code I got a “rock wins”.

YOU ARE BRILLIANT! Thank you so much!!!

Now if you are clear about the function call, arguments/parameters. Then have a look inside the function and try to compare choice 1 and choice 2. Only at the start of your function you have compared . You wrote[quote=“mcordero1206, post:9, topic:39955”]
if(choice1 === choice2){
return “The result is a tie!”;
[/quote]

The rest of the code inside the function you did not compare.[quote=“mcordero1206, post:9, topic:39955”]
else if(choice2 === “rock”){
return “rock wins!”;
if(choice2 === “scissors”){
return “scissor wins!”;
}
[/quote]

You are only stating choice 2.

To use as a guideline, how you can compare, see the code below:

else if(choice1 === “rock”)
{
if(choice2 === “scissors”)
return “rock wins”;
else
return “paper wins”;

  }

So should I change this like so?

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