javascript - Syntax Error, Illegal return statement - Stack Overflow

Hi I have this code right now var pare = function (choice1, choice2){if (choice1 === choice2){return &

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744908918a4600426.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信