Help me! What am I doing wrong?


#1



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

This keeps giving me a syntax error help!


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

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

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

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


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

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


#2

this bit of code:

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

you only need once at the beginning. Inside your compare function, things aren't suddenly going to change, if you checked for a tie once, it should be enough.

Look at this code:

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

the if and else should be inside your if statement:

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

if the user (choice1) picks rock, you want to check if the computer has scissors, because then you win, if this is not the case, the computer has paper, and wins.

Take a close look at how you nested your if/else if and else statements


#3

So I did that, thanks, but it's still giving me a syntax error!! Here's what my code looks like now:

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 == "paper") {
if(choice2 == "scissors") {
return "scissors wins";
};
compare(userchoice, computerchoice)

Advice??


#4

could you post your full code?


#5

Sure.

/*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 == "paper") {
if(choice2 == "scissors") {
return "scissors wins";
};
compare(userchoice, computerchoice)

Advice???


#6

you have two else if(choice1 == "paper") , shouldn't the second one be choice1 == "scissors", and this else if statement, should have a if and else inside the else if


#7

So should it should be

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

????


#8

no, it should be the same as the others:

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

#9

Now it's saying that there's an unexpected end of script. But WHERE??

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

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

Sorry it's been so long, I haven't had time to code


#10

your function opening curly bracket doesn't have a matching closing curly bracket


#11

Where?? I'm sorry i'm being so annoying but I am having a lot of trouble


#12

your function:

var compare = function (choice1, choice2){

the { doesn't have a matching } at the end of the file


#13

There's nowhere to put it!!

I tried putting it after the return, but then it doesn't recognize the else if statement

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

It doesn't make any sense


#14

i said end of file... All the other if and else statements should still be inside the function


#15

I GOT IT NOW THANK YOU SO MUCH FOR YOUR PATIENCE :grinning::heart_eyes::kissing_heart::grin: