If choice1 === "rock" syntax error


#1

I've read everything and I still can't figure out what I'm doing wrong. All I get is that there is a problem with my syntax. Help please!

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


#2

You are getting lost in the Curly-Bracket-{ }-Forest.

You are assigning a so-called anonymous Function
to the variable compare.

var compare = function( choice1,choice2 ) {
            //Begin of FUNCTION-BODY

            //Your code

            //End of FUNCTION-BODY
   };

It is in the FUNCTION-BODY
that you place
an IF-Statement with it's IF-codeblock
and
one or more ELSE-IF Statements with their code-block.

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

  • the build of the IF ELSE-IF statement structure

#3

I appreciate your help, but I still don't understand. I feel as though I am doing everything that you say to do. Is there any more insight you can give?

Thanks!


#4

Well how about I dissect your code real quick for you and perhaps we can get to a solution together!

Your Code:

//Your Code
var compare = function(choice1, choice2) //Missing open bracket
  if (choice1 === choice2) {  //One open bracket
    return "The result is a tie";
  } //Closed bracket

  else if(choice1 === "rock") { // One Open bracket!
     if(choice2 === "scissors") { // Two Open brackets!
       return"rock wins";
     } // One Open bracket
     else { // Two Open brackets
        return "paper wins";
     } // One Open bracket
} // Zero Open Brackets

Right now you're missing an open and closing bracket for your function and it's causing further errors

So what @leonhard_wettengmx_n means when you're assigning an anonymous Function He means you are not using opening and closing curly brackets to create a FUNCTION-BODY. I hope I explained this thoroughly enough for you!


#6

Someone please tell me where I am going wrong!

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

}

#7

It seesm to be the same code as above, so the answer remains the same:

You need to close the function:

var compare = function(choice1, choice2) { // opens function
    if(choice1 === choice2) { // opens if
        return "The result is a tie!";
    } // closes if
    else if(choice1 === "rock") { // opens else if
        if(choice2 === "scissors") { // opens if
            return "rock wins";
        } // closes if
        else { // opens else
            return "paper wins";
        } // closes else
    } // closes else if

// as you can see the function is still open.

#8

still does not work so i don't know where I am going wrong


#9

@ladylee1,
Present the FULL code you are now using.....and mention the EXACT Oops- or Error-message you are getting !!!


#10

There is the full 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 === choise2) {
return "The result is a tie!";
}
else if(choise1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
};

And this is the error
Oops, try again.
Did you create a function called compare?


#11

Everything is there???


#12

@katutz,

Have a close look at

choise2