gpt4 book ai didi

Javascript 函数在调用时不运行

转载 作者:行者123 更新时间:2023-11-28 18:30:26 25 4
gpt4 key购买 nike

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”函数,这意味着它应该向用户请求新的输入,并从计算机获取新的输入。

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

最佳答案

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

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

关于Javascript 函数在调用时不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com