admin管理员组

文章数量:1026989

Hi I have this code right now

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

if (choice1 === "Rock")
{
    if (choice2 === "Scissors")
    {
        return "Rock wins!";
    }
    else (choice2 = "Paper")
    {
        return "Paper wins!";
    }
}

but I keep getting a Synatax error, illegal return statement.

I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.

I am using an online editor by the way, not an actual IDE.

Hi I have this code right now

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

if (choice1 === "Rock")
{
    if (choice2 === "Scissors")
    {
        return "Rock wins!";
    }
    else (choice2 = "Paper")
    {
        return "Paper wins!";
    }
}

but I keep getting a Synatax error, illegal return statement.

I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.

I am using an online editor by the way, not an actual IDE.

Share edited Dec 18, 2013 at 8:49 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Dec 18, 2013 at 8:03 Ankur JainAnkur Jain 151 gold badge2 silver badges8 bronze badges 4
  • 1 choice2 = "Paper" looks like a typo – Bathsheba Commented Dec 18, 2013 at 8:05
  • 2 Maybe you wanted else if(choice2 == "Paper")? - and wrap the if inside the pare function :) – Niccolò Campolungo Commented Dec 18, 2013 at 8:06
  • Go cheat on your classmate : stackoverflow./q/20622970/1636522 :D – user1636522 Commented Dec 18, 2013 at 8:08
  • Thanks LightStyle, that did it! – Ankur Jain Commented Dec 18, 2013 at 8:46
Add a ment  | 

3 Answers 3

Reset to default 2

I think you want to write the following

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

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return "Rock wins!";
        }
        else if (choice2 === "Paper")
        {
            return "Paper wins!";
        }
    }
};

return statement is only valid inside the function.

Look at the code

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

this part of the code will work fine. but after this semicolon (;) a separate part of the code is started which eventually is not the part of function as you function ended with the semicolon, that's why lower part of the code is giving this error.

Second thing, Your else statement makes no sense. You code should be like this

var pare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    if (choice1 === "Rock") {
        if (choice2 === "Scissors") {
            return "Rock wins!";
        } else if (choice2 === "Paper") {
            return "Paper wins!";
        }
    }
};

Also, I made this mistake, when using debugger; or line break somewhere, don't return in your browser's console. This returns the same syntax error because your return is not wrapped in a function.

In console don't:
return choice1 === choice2 // error

Instead just do:
choice1 === choice2 // true or false

Hi I have this code right now

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

if (choice1 === "Rock")
{
    if (choice2 === "Scissors")
    {
        return "Rock wins!";
    }
    else (choice2 = "Paper")
    {
        return "Paper wins!";
    }
}

but I keep getting a Synatax error, illegal return statement.

I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.

I am using an online editor by the way, not an actual IDE.

Hi I have this code right now

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

if (choice1 === "Rock")
{
    if (choice2 === "Scissors")
    {
        return "Rock wins!";
    }
    else (choice2 = "Paper")
    {
        return "Paper wins!";
    }
}

but I keep getting a Synatax error, illegal return statement.

I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.

I am using an online editor by the way, not an actual IDE.

Share edited Dec 18, 2013 at 8:49 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Dec 18, 2013 at 8:03 Ankur JainAnkur Jain 151 gold badge2 silver badges8 bronze badges 4
  • 1 choice2 = "Paper" looks like a typo – Bathsheba Commented Dec 18, 2013 at 8:05
  • 2 Maybe you wanted else if(choice2 == "Paper")? - and wrap the if inside the pare function :) – Niccolò Campolungo Commented Dec 18, 2013 at 8:06
  • Go cheat on your classmate : stackoverflow./q/20622970/1636522 :D – user1636522 Commented Dec 18, 2013 at 8:08
  • Thanks LightStyle, that did it! – Ankur Jain Commented Dec 18, 2013 at 8:46
Add a ment  | 

3 Answers 3

Reset to default 2

I think you want to write the following

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

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return "Rock wins!";
        }
        else if (choice2 === "Paper")
        {
            return "Paper wins!";
        }
    }
};

return statement is only valid inside the function.

Look at the code

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

this part of the code will work fine. but after this semicolon (;) a separate part of the code is started which eventually is not the part of function as you function ended with the semicolon, that's why lower part of the code is giving this error.

Second thing, Your else statement makes no sense. You code should be like this

var pare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    if (choice1 === "Rock") {
        if (choice2 === "Scissors") {
            return "Rock wins!";
        } else if (choice2 === "Paper") {
            return "Paper wins!";
        }
    }
};

Also, I made this mistake, when using debugger; or line break somewhere, don't return in your browser's console. This returns the same syntax error because your return is not wrapped in a function.

In console don't:
return choice1 === choice2 // error

Instead just do:
choice1 === choice2 // true or false

本文标签: javascriptSyntax errorIllegal return statementStack Overflow