调用

时间:2016-06-30 07:19:07

标签: javascript

var gameFunction = function()
{
    var userChoice = prompt("What do you choose: rock, paper,   or scissors?")

    var computerChoice = Math.random();

    if (0 < computerChoice < 0.33)
    {
        computerChoice = "rock";
    }
    else if (0.33 < computerChoice < 0.67)
    {
        computerChoice = "scissors";
    }
    else
    {
        computerChoice = "paper";
    }

    console.log("Computer choice: ",computerChoice)

    if (userChoice === computerChoice)
    {
        return "The result is a tie! Enter a new result?"
        gameFunction();
    }

    else if (userChoice === "rock")
    {
        if (computerChoice === "scissors")
        {
            return "rock wins"
        }
        else if (computerChoice === "paper")
        {
        return "paper wins"
        }
    }

    else if (userChoice === "paper")
    {
        if (computerChoice === "rock")
        {
            return "paper wins"
        }
        else if (computerChoice === "scissors")
        {
            return "scissors win"
        }
    }

    else if (userChoice === "scissors")
    {
        if (computerChoice === "paper")
        {
            return "scissors wins"
        }
        else if (computerChoice === "rock")
        {
            return "rock win"
        }
    }
}

gameFunction();

这是Codecademy的“Rock paper scissors”游戏的9/9部分:Javascript。

我的问题是:

当用户和计算机绑定时,它应该重新运行整个“gameFunction”功能,这意味着它应该询问用户的新输入并从计算机获取新输入。

然而,程序只打印出“结果是平局!”没有重新运行“gameFunction”。我该如何解决这个问题?

6 个答案:

答案 0 :(得分:1)

返回语句后没有执行行..尝试

 gameFunction();
 return "The result is a tie! Enter a new result?"

答案 1 :(得分:1)

return语句退出&#34; gameFunction&#34;功能所以它不会执行下一行。尝试使用提示,而不是这样:

if (userChoice === computerChoice)
{
    prompt("The result is a tie! Enter a new result?");
    gameFunction();
}

这样,用户可以响应您的提示,您可以使用它来决定游戏是否继续。您也可以随时使用警报:)

答案 2 :(得分:1)

gameFunction()里面的recofive gameFunction()方法因为 control返回第一个调用函数。

if (userChoice === computerChoice)
{
    return "The result is a tie! Enter a new result?"
    gameFunction();
}

因此,您可以在那里打印一条消息,表明存在平局。

if (userChoice === computerChoice)
{
    alert("The result is a tie! Enter a new result?")
    gameFunction();
}

当不满足上述条件时,它只是返回到呼叫区域并停止。

答案 3 :(得分:0)

将返回更改为alert(),如下所示:

自:

return "The result is a tie! Enter a new result?"

要:

alert("The result is a tie! Enter a new result?");

答案 4 :(得分:0)

怎么样:https://jsfiddle.net/41gcfL6g/

这里的事情是在函数中添加一个参数,这样你就可以确定你上次玩的时候是不是一个平局。然后在tie的情况下,而不是在返回后调用函数,返回gameFunction的结果

答案 5 :(得分:0)

请参阅小提琴,不要使用return,console.log https://jsfiddle.net/o62vda05/

var userChoice;

function startGame()
{
    userChoice = prompt("What do you choose: rock, paper,   or scissors?");
    gameFunction(); 
}

function gameFunction()
{
    var computerChoice = Math.random();

    if (0 < computerChoice < 0.33)
    {
        computerChoice = "rock";
    }
    else if (0.33 < computerChoice < 0.67)
    {
        computerChoice = "scissors";
    }
    else
    {
        computerChoice = "paper";
    }

    console.log("Computer choice: ",computerChoice)

    if (userChoice === computerChoice) {
        console.log( "The result is a tie! Enter a new result?");
        startGame();
    } else if (userChoice === "rock") {
        if (computerChoice === "scissors")
        {
            console.log( "rock wins");
        }
        else if (computerChoice === "paper")
        {
            console.log( "paper wins");
        }
    } else if (userChoice === "paper") {
        if (computerChoice === "rock") {
            console.log( "paper wins");
        } else if (computerChoice === "scissors") {
            console.log ("scissors win");
        }
    } else if (userChoice === "scissors") {
        if (computerChoice === "paper") {
            console.log ("scissors wins");
        } else if (computerChoice === "rock") {
            console.log( "rock win");
        }
    }
}

startGame();