6. Syntax Error: Return statements are only valid in side functions


#1


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!";
    }
};
if(choice1 === choice2) {
    return "The result is a tie";
}

else if(choice1 === "rock") {

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

}


My result for this code is Syntax Error: Return statements are only valid in side functions.
What did I do incorrectly?


#2

@scriptmaster57182,

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

You used a semi-colon-; in your IF ELSE-IF statement.
For Javascript the semi-coln-; is an End-of-Statement indicator
in this case it would be the END of the Statement

http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements


#3

I have the same code except it keeps on popping up with Syntax Error: Unexpected token else. The code seems right to me.


#4

Here is my new code and it gives me, SyntaxError: Expected token '{'

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";
}
[quote="scriptmaster57182, post:1, topic:29886, full:true"]


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!";
    }
};
if(choice1 === choice2) {
    return "The result is a tie";
}

else if(choice1 === "rock") {

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

}


My result for this code is Syntax Error: Return statements are only valid in side functions.
What did I do incorrectly?
[/quote]


#5

Hi here

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
}; <== this one

You should remove one } after your if statement because it close the compare function but you should close your compare function after your else if statement like that

else if(choice1 === "rock") {

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

}
} <== add one here for closing the compare function

And here


else { computerChoice = "scissors"; console.log("Computer: " + computerChoice); }

Try to put the console.log() outside the else statement.