What if paper is choice 1?


#1

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


My code keeps on telling me I have a syntax error but I cant seem to find the problem.


Replace this line with your code.



#2

Well lets count your { }. I see 3 { but 4 } try and match up each } with a {


#3

you create an else if for one of the possible choices of choice1, then inside else if you should create an if and else for the possible options of choice2

you didn't nest the if and else inside the else if:

else if (choice1 === "paper"){
   /* if and else should be here */
}

#4

I got rid of the 2nd { where the ELSE IF statement is and continued with the rest of the IF and ELSE statement but it still has the same problem?


#5

can we see an updated version of your code?


#6

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

you know did it right when choice1 is paper, but not when choice1 is rock, why? Why are the if and else which check for choice2 not nested inside else if when choice1 is rock?

also, look at this comment:

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

// missing } to close else if
// missing } to close function

#8

the reason why it is not nested inside else if when choice 1 is rock is that because I am trying to work out every possible outcome. also I followed both your comments and still no results. this is a little confusing since I have followed the steps of the instructions


#9

but to cover every possible outcome, in this course we build 3 else if conditions to check what the user (choice1) picked

for each possible choice of the user, we see what the computer picked (if and else), you should follow this structure, again, if you need more help, post an updated version of your code


#10

I don't understand what I have done wrong. where do you want me to put the IF and ELSE statement which is in the 2nd ELSE IF in order to work out the outcome if the choice1 is equal to paper? my code is still exactly the same as the first image I posted.


#11

I nested the if else statement within the else if statement, ensuring to close the else if statement with a {. in the end of the code I have included the }; to close the function

this is how it looks. can you please find the mistake I am making, I have tried different ways of fixing this but still no result.

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

};


#12

here:

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

if and else should be nested inside else if to determine who won when user picks rock


#13

I have followed everything you told me. I believe it is right but this is what I keep getting?


#14

also the part you would like me to correct I have already done. I am trying to work out the scenario of what if choice1 is paper and not if choice1 is rock. I hope this helps you understand where I'm struggling at.


#15

i just don't know how to explain it any more? We create else if to check for possible scenario of choice1:

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

} // closes else if

once we determined what the user (choice1) picked, we need to see who won, so we are going to see what the computer choice:

/* correct code */
else if (choice1 === "paper"){
    if (choice2 === "rock")  {
        return "paper wins" }
    else {
        return "scissors wins";
    }
} // closes else if

now look at your code:

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

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

see the difference?

the design idea of the program is:
check if result is a tie
else if check if user picked rock, see what computer picked and determine winner
else if check if user picked paper, see what computer picked and determine winner
else if check if user picked scissors, see what computer picked and determine winner

but to achieve this, we need to nest an if and else inside each else if to determine who won after we determined with the else if condition to see what the user picked


#16

I understand that I need to nest the IF/ELSE statement within the ELSE IF which I have followed. Here is my code after following what you have done. clearly the code is not incorrect but there may be a different problem as I am still coming out with a syntax error.


#17

@codexthon is right, you say you understanding the nesting, but clearly this is not the case


#18

Hi @digitalsurfer10060, based on your answer:

In line 17, delete the }
(reason: you closed the else if condition too early without any code inside, notice you started { in line 16, but you close it with } without any code in between.)

In line 23, add the }
(reason: you need to close the else if condition after running all the codes, the } in line 22 is for the else condition, so you need another } for the overall else if condition.)

then your code should be working.


Unexpected token else
Another exercise 12 functions if else problem
#19

Thank you so very much. My code has finally worked!! ;D You guys have been amazing help and given terrific support towards me. Greatly appreciated.


#20