Syntax error Rock Paper Scissors Please Help


#1

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

syntax error : return not in function


 

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


#2

Look carefully at this section of your code. Check all of your if and else if statements for syntax errors.


#3

so my if/else statement is in the wrong format?


#4

This is what your syntax for all your else if statements should be for this exercise. This is code you wrote. Use it as a template for your remaining else if statements.


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

SyntaxError: Unexpected token else

How can I fix this Syntax error???
Thanks for help


#6

var compare = function (choice1,choice2){
if(choice1 === choice2){
return "The result is a tie!";}  // here you closed if 
}; //  why did you close function here? remove it

close the function after closing else statement


#7

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

I changed the code like this however sytnax error is still same :frowning:


#8

now you missed a }
you forgot to close if satement


#9

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

I already closed the if statement.


#10

No you didn't close.see your code what you posted.and make sure you close the function too


#11

thanks I got it rn:))


#12

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