No surprise - stuck!


#1

I don't get it! What's wrong here? Keep getting Illegal return statement.

/*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(userChoice,computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
}
};

compare(userChoice,computerChoice);{
if (userChoice === "rock") {
if (computerChoice === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if (userChoice === "paper") {
if (computerChoice === "scissors")
{
return "scissors wins";
}
else {
return "paper wins";
}
}
else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
}
else {
return "scissors win";
}
}
}


#2

@objectpro26291,

With

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

you have closed the compare function.....

Then
you start to define a 2nd compare function ( overwriting the 1st )

compare(userChoice,computerChoice);

In your 2nd compare function definition
you use a semi-colon-; directly after your parameter-holders ( )
causing the rest of the definition
not to be seen as a part of this 2nd compare function definition, as the function is ended.

A return statement outside of a function-definition is invalid.....

A semi-colon-; is seen by the Javascript Interpreter as an End-of-Statement indicator.

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 )