Javascript函数在调用时不运行

Javascript function does not run when called

本文关键字:运行 调用 函数 Javascript      更新时间:2023-09-26
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: Javascript的"石头剪刀布"游戏的9/9部分。

我的问题是:

当用户和计算机连接时,它应该重新运行整个"gameFunction"函数,这意味着它应该向用户请求一个新的输入,并从计算机获得一个新的输入。

然而,程序只是输出" the result is a tie!"而没有重新运行"gameFunction "。我该如何解决这个问题?

return语句后不执行任何行。试着

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

return语句退出"gameFunction"函数,因此它不会执行下一行。尝试使用这样的提示符:

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

这样用户就可以回应你的提示,你可以用它来决定游戏是否继续。你也可以直接使用警告:)

将return改为alert(),如下所示:

来自:

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

:

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

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

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

的结果

gameFunction()中的隐式gameFunction()方法,因为控制返回到第一个调用函数。

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();
}

,当上述条件不满足时,则返回到调用区域并停止。

查看,不要使用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();