Stuck at 7. What if choice1 is paper? I've tried many things... and nothing


#1

The error it displais is "SyntaxError: Unexpected token else". I have tried to remove and change semicolons, parenthesis. But I always receive an error. Thank you in advance!

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

No semicolons after conditional statements for example before the paper else if:


#2

@dericm,

Do NOT use semi-colon's-; in your IF ELSE-IF statement structure....
For the Javascript Interpreter a semi-colon-; is an End-of-Statement indicator !!

You are using 1 closing-curly-bracket-} to many in your ELSE-IF structure...

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#3

Thank you very much but after making some changes like removing the semicolon and changing the closing-curly-brackets.But I still receive the same error.

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


#4

@dericm,

You are still using 1 closing-curly-bracket-} to many.....

The ELSE-IF structure should look like:

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

Have a second read through the section

•the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY

#6

I see the point. But for instance in the exercise 6, they accept the 3 closing-curly-brackets at the end. Nevertheless they don't accept the same structure for the exercise 7, even though the task is the same.

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

@dericm,

You are confronted with the =code-checker=
which in this case is using a =functionality=-test.

You have to be aware
that with

var compare = function(choice1,choice2) { FUNCTION-BODY };

you are defining a compare variable
to which you assign a so-called anonymous Function
which takes 2 so-called parameter choice1 and choice2

Within the =FUNCTION-BODY=
the choice1 and choice2 parameter
are used as so-called =local= variables

You then use these =local= variables choice1 and _choice2
in an IF ELSE-IF statement structure
using the comparison operator in the IF and ELSE-IF_condition_

The most common syntax problem
is a =missing closing-curly-bracket-}=
which is used to close / terminate the FUNCTION-BODY.....

If you followed the instructions
the following compare-code-test should give a true in every case....

console.log("choice1 && choice2  argument's are equal");
console.log("1 "+ (compare("paper","paper")==="The result is a tie!"));
console.log("2 "+ (compare("rock","rock")==="The result is a tie!"));
console.log("3 "+ (compare("scissors","scissors")==="The result is a tie!"));

console.log("choice1 argument 'paper'");
console.log("4 "+ (compare("paper","rock")==="paper wins"));
console.log("5 "+ (compare("paper","scissors")==="scissors wins"));

console.log("choice1 argument 'rock'");
console.log("6 "+ (compare("rock","paper")==="paper wins"));
console.log("7 "+ (compare("rock","scissors")==="rock wins"));

console.log("choice1 argument 'scissors'");
console.log("8 "+ (compare("scissors","paper")==="scissors wins"));
console.log("9 "+ (compare("scissors","rock")==="rock wins"));

#8

@dericm,

### the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#9

I think you're missing a bracket at the end of your code. What helped me to spot that out with mine, is reading it thoroughly, making sure that the bracket had its correspondent end. Also, You could always count how many brackets you have. If its odd, then you're most likely missing one or have too many.