What if choice1 is rock?


#1

Hiya,

Im getting stuck at this part. Here is my code:

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

Can anyone see where I have made the mistake?


#2

Hi @methodslayer11843

Please ensure to paste all your code and any error messages you are getting so someone can effectively help you :smile:
With that in mind, I would hazard a guess right now that you will want to move your final else statement into your else if code block so it evaluates the outcome of choosing rock.
An example is below. If this fails please do paste your entire code and the error and someone will try to help as soon as they can :smile:

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 { //we move the else into the else if 
            return "paper wins";
        }
    }
}

#3

Oh sorry, didn't realise. I tried your suggestion and it didnt work unfortunately so please see below:

Error: Oops, try again. There was a problem with your syntax.

Instructions:
Let's code our outline from above:

Inside the compare() function under the existing code, write an else if statement where the condition is choice1 === "rock".
Inside this else if statement, write an if / else statement. If choice2 === "scissors", return "rock wins". Else, return "paper wins".


#4

Oh and here is the syntax error:

SyntaxError: Unexpected end of input


#5

@methodslayer11843, thanks for you patience on my reply :smile: , would you mind pasting your code in it's entirety if you are still getting the error?


#6

No problem, thanks for your help:

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

}


#7

Thanks @methodslayer11843,
please see the code example below with comments if it may help.

In short you were victim to the syntax gremlins :wink:, there was an extra * symbol not needed or in use, and a missing paren to close your function out properly.

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);// lets remove the * symbol from here to start
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";
        }
    } //this paren belongs to the last else if
} //let's add one more to close the function

console.log(compare(userChoice, computerChoice)); //let's test it

#8

Hey,

Thanks so much, that did it :smile:


#9

i have done everything right.still i see this error plz help me out.thanks


#10

Hi @rubyplayer65007,

the same thing for you as methodslayer11843, could you please post your code you are working with if you are still having issues, as the above thread hopefully shows once you get the code in the mix things generally become easier to debug :smile:


#11

Hey @rubyplayer65007,

You put one of the things like this:

if/if

When it's supposed to be:

if/else if

You have to change one of your loops (The one that has "choice1 === rock").

Also, you forgot a bracket on the bottom. :smile:

Actually, it just that we might not be able to see it. There should be 3 brackets at the bottom of your code.

1)Function bracket
2)Else if bracket
3)Else bracket


#12

This one had me confused too:

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

}

}


#13

@1martinaw,

I'm not sure if you're throwing in your 2 cents here or asking for help heehee :laughing:
If you are getting any errors please let us know what so we can try to help if not and you are also trying to help then great stuff ad you may disregard, just didn't want this to get buried if you needed a hand.