Choice 1 = Paper syntax error


#1


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

I've been tweaking my coding based on various posts I've seen on the forum, and I either get an unexpected 'else' message, or the current one: "SyntaxError: Return statements are only valid inside functions." Could I get some advice, please?

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

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


#2

Your 2nd closing curly brace ends your compare function here and seals it off from the rest of your code.


#3

I removed the second curly brace, and now I'm getting "Unexpected end of script"?


#4

This looks different than the code you had in the prior exercise. There's a word missing.


#5

Which word is missing? I've been staring at my code and I can't seem to find it


#6

Taken directly from the hints of the previous exercise:

Inside the compare() function under the existing code, write another else if statement where the condition is choice1 === "paper".


#7

Trying the following got me "Unexpected keyword 'else'"

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

};


#8

Erase that else from:

I meant for the else to go:


#9

Ah, sorry I missed that. I tried to fix it the way you put it, and I'm getting the "SyntaxError: Unexpected keyword 'else'" problem again?


#10

Can you post your current code? We're close to you solving this.


#11

The current code is what you suggested, but here it is, pasted for convenience. :smile:

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


#12

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

else if (choice1 === "paper") {
delete the else here else if (choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}; remove the semi colon here

now, properly close your function


#13

I think I'm going to need to review an earlier lesson, because now I'm getting "SyntaxError: Unexpected keyword 'else'. Parse error." and my notes on closing a function don't seem to be helping me out here


#14

sample function syntax

Var functionname = function(input){
Code code code;
}

@zeenovos you are very close to a solution


#15

I figured out that I was missing a third ending bracket! Thank you for your help and your patience. :smile:


#16

I'm glad you were determined :smile:


#17

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