Lesson 7:What if choice1 is paper?


#1


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


what have I done wrong because i get error massage
SyntaxError: Syntax error


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


#2

I can see a missing curly bracket before one of your returns. Also both of your 'else if's are opened but not closed.


#3

nope, i get same error


#4

You are closer to the result. Please can you put your code up.


#5

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


#6

you got some more grammatical errors so I would fix it like below:
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}}
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins";
}}
else {
return "scissors wins"
}
}


#7

@astrophe717 @bibleman13 now i get a error massage that say

SyntaxError: Expected '}'


#8

You have opened 8 curly brackets and only closed 6. Look at your code like it is made primarily from the if, else if, else if blocks. Look at your curly brackets to figure out which two of these three blocks still need to be closed.


#9

@rcodeman @astrophe717 @albionsrefuge @stetim94 I get New massage that says

Oops, try again. Your code returned 'rock wins' instead of 'scissors wins' when the inputs are paper and scissors


#10

Please post your code.


#11

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


#12

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

this section should contain the if

}


#13

thanks for Your help it woked . Thanks for all the help I got you are AWESMOME :wink:


#14

Here is my belated and rather long effort at clarifying javaScript functions.

Before you worry to much about the code, think carefully about how the game works. For example take the case that choice1 is paper. choice2 can either be rock, paper or scissors. If choice2 is paper then the game is a tie. If choice2 is rock then paper wins and if choice2 is scissors then scissors wins.

After you have said there is going to be some function, the way the function gets written is:
(a) ( the condition e.g. if exactly one thing happens. The condition is enclosed in parentheses to show that it is the condition. )
(b) { what the function does e.g. return "rock wins";. This is enclosed in curly brackets to show it is the code to be executed. }.

Your code should resemble the following format ( # indicates my comments)...

var functionName = function (parameters i.e. what does the function depend on) {

# the functions 'to do' list goes in here. For example:

if (choice1 === 'chicken' {
Do something. You might need more conditions first...
if ('chicken' < 2) {
return 'There is not enough chicken for you and your friends.';
}
else if ( 'chicken' < 6) {
return 'There is enough chicken for you and some friends';
}
else {
return 'You have enough chicken for a small party';
}
} # here you are closing the original 'if'.

else if (choice === 'lentils') {
# either set further conditions and/or execute code;
}

else {
return 'You are not choosing chicken or lentils. I hope you know what you're doing!";
}

# The function is not finished until you close the original curly bracket on the first line of the function. So...

}


#15

You mad it more Clear for me thanks.
I did not knew that you need to Close the orginal if


#16

It can be time consuming to debug missed parentheses and curly brackets.


#17

2 posts were split to a new topic: 8. what if choice1 rock