6. What if choice one is rock?


#1

I have this 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";
    }
};

It doesn't work! I can't figure it out!


#2

The problem there @computergeek2015, is that you have your else if staement to close to early so do this:

On this piece of your code(Added coments):

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

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

else {
    return "paper wins";
}
}
};

Hope this helped!


#3

Still wrong this is the error:

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

This is what it say:
SyntaxError: Unexpected token else


#4

@amanuel2, I'm unsure as to that working. The else goes to that if statement, and not to the else if statement. Putting the else inside the if will, in itself, cause an error.

@computergeek2015, could you post all of the code from your program? If that's all the code you have then that's your problem, you need to keep the code from the previous lessons to allow this to work.


#5

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


#6

} //<--- Delete one of these but not both
} //<--- Delete one of these but not both
};

#7

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

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

This still doesn't work can I please have help.


#8

for some reason if I do nothing i will pass the lesson can I please have some help here


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

}
}
};
here is the correct code for you guys :smile:


#10

Here you go

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

#11

@amanuel2
You forgot to close your if statement


#12

still doesn't work. Please help!


#13

Still doesn't work please help


#14

Also had a same problem. The post from @the_s10 is correct thus it works. Now, as for you @ocopplestone I think the problem is in { . They are often making problem as it happens that we don't put them on the right places.
We have else if statement that has begining { after var line:

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

Now, inside this else if statement we have 1 if and 1 else statement. They both have their own { and }. Now this part is a little bit tricky. Look at this lines:

else if(choice1 === "rock") "{"
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
"}"
}
You can see that if statement ends in the second from above line...
As for the last "}" it actual ends the whole function (remember that the begining, the first { was after "var compare = function(choice1, choice2)")

So theres your explenation why you have to put 3 "}" at the end...

Hope this helps you figure it out :smile:


#15

Thank you so much but what is the real working code?
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
}
is this it?


#16

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

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

else {
return "scissors wins";
}
}
};
This doesn't work


#17

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

}
}
};

re-did the exercise and this works


Note you should not see anything on the console.

Hope this helped :smile: