Build "rock, paper and scissors" not working (SyntaxError: Illegal return statement)

spanish

#1

//HOLA, ESTOY TENIENDO UN PROBLEMA CON MI CODIGO EN EL EJERCICIO DE BUILD ROCK PAPER AND SCISSORS.
-No encuentro el error en el codigo. (SyntaxError: Illegal return statement)

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if (computerChoice <= 0.33) {
computerChoice = "rock";
} else if(computerChoice <= 0.66) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}

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

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

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


#2

@devpro22602,
A return statement can only be used in a function-body....
and as you write

var compare = function(choice1, choice2) {
   if (choice1 === choice2){
      return "The result is a tie!";
   }
   if (choice1 === "rock"){
      if (choice2 === "scissors"){
         return "rock wins.";
      } else {
         return "paper wins";
      }
   }
// closing the FUNCTION-BODY
};

the compare =function= is allready closed !!!!

Furthermore you should be using an IF ELSE-IF statement structure.....

please read
http://www.codecademy.com/forum_questions/549771e79113cb428d005ef3
with special attention to
-1 the return strings expected
-2 the IF ELSE-IF statement structure
-3 the execution of the compare function

References::

http://javascript.crockford.com/code.html <== code convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements
http://stackoverflow.com/questions/25101936/why-does-adding-a-semicolon-sometimes-break-an-if-statement


#3

Thanks men :smiley: really helpfull


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.