Javascript函数在调用时不运行
Javascript function does not run when called
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();
相关文章:
- 运行Infinite Scroll后调用函数时出现问题
- 重新运行上次进行的AJAX调用
- For循环在调用时未运行
- AJAX调用运行C代码的最佳实践
- HTML5(Bootstrap)通过函数调用运行动画
- 在jquery中完成ajax调用后,如何运行一些代码
- 防止函数在从ajax再次调用后多次运行
- 如何显示由ajax调用运行的PHP文件的echo
- Ajax调用在InternetExplorer中冻结UI,但在firefox中运行良好
- 通过Wordpress中的jquery调用运行php文件
- JavaScript只有在最后一次被调用时才运行函数
- 在异步AJAX调用完成后,通过变量运行函数
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 运行一次函数,然后在 5 秒内禁止进一步调用
- 运行 AJAX 需要什么 - 将 jQuery 添加到页面处理 AJAX 调用
- 从 jquery.ready() 调用的 javascript 何时相对于 中的脚本运行
- 如何使用仅在运行时已知的变量名称调用 JavaScript 函数
- 如何运行一个调用某个函数的函数首先完成它,然后再调用下一个函数.正在创建回调函数
- 动态弹出窗口'通过调用一个永远运行并返回's每个周期后的数据
- 如何从 html 文件运行/调用 javascript 文件中的 javascript 函数