8 what is choice1 is scissors


#1


hey guys! I have no idea where i went wrong at! please help! Thanks!!!!

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

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

}else if (choice1 === "rock"){
    if(choice2 === "scissors") {return "rock wins";}
    else {return "scissors wins";}
 }  
};
}
compare(userChoice, computerChoice);


#2

^ this condition choice1 === choice2 its in curly brackets { } its supposed to be in ( )


#3

Hey zainabrawat, Thanks for the reply, I've changed that but it says "SyntaxError: Unexpected token else", what should i do?


#4

@thejoe98
Your code seems to be a little hard to read, can you edit it it a little bit?
like

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

instead of

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

and other stuff like that. That well make it a little easier to read.:grin:

As for your problem, it probably is an else statement that is causing it.


#5

Thanks for the reply printcoder_eric, I've changed the codes, however it stills says unexpected token else, I played around with the code but still, no luck, what I have found was in line 20 when i cancel out the else, the message comes up with unexpected token if... so I'm assuming that the problem is somewhere in line 20

thanks


#6

Please paste in your code, its hard to debug from a picture


#8

@zainabrawat Should I put down the code again?


#9

Still struggling with the final part of this exercise. Would really love to finish this exercise. Here is my code so far:
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 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
compare(userChoice,computerChoice);


#10

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

compare(userChoice, computerChoice);

What the heck is wrong with this?


#11

Hi you should close the compare function at the end with a } after the closing } of the last else if statement

else if(choice1 === "scissors") {
    if(choice2 ==="rock") {
        return "rock wins";
    }
    else{
        return "scissors wins";
    }
};
}<== addd one here

#12

Great! Thanks a lot :slight_smile:


#13

HI this part you should remove one } because it close the compare function before

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

then add one at the end here

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

for closing the compare function


#14

@thejoe98 @codejumper43464
Hey, still need help?


#15

@wizmarco I got it figured out now. Thank you very much for the help. I truly appreciate it. I'm starting to get better with Javascript.


#16

I have similar problem, and I can not figure it out. Can someone see a mistake?
Here's 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);

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


#17

HI in your last else if statement you put a condition to the else statement remove it. and inside it you have

return === "scissors wins";

remove the ===


#18

Thank You. I removed this silly mistake, but it does not work at the end...


#19

can you post your new code?


#20

Yes, here it goes:
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";
}

    };

}
compare (userChoice, computerChoice);


#21

I said the else statement doesn't take a condition

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

remove the (choice2=== "paper")