如何在相同的选择后停止循环
How to stop the loop after same choices?
我的代码看起来像这样,但我有一个问题。只要选择相同,游戏就会重新开始。在完成一个游戏后,我得到了一个"未定义"的警报,因为startGame()函数使用第一轮的参数再次运行(其中的选择是相同的)。
我是JS这个词的新手,希望尽可能简单。有人能帮我提供一个如何停止"未定义"的解决方案吗?
// the game starts here
function startGame() {
// the user
var userChoice = prompt("Do you choose rock, paper or scissors?");
// if invalid input
while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
userChoice = prompt("Please select again, this time correctly!");
}
alert("You chose " + userChoice);
// computer
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
alert("Computer pick: " + computerChoice);
// comparison between user and computer
var compare = function(choice1, choice2) {
// if both selections are the same
if (choice1 == choice2) {
alert("Tie, restart the game!");
startGame();
}
// if the user selects rock
else if (choice1 == "rock") {
if (choice2 == "scissors") {
return "You have won the game!";
} else {
return "LOOOOOSSEEER!";
}
}
// if the user selects scissors
else if (choice1 == "scissors") {
if (choice2 == "rock") {
return "You have won the game!";
}
} else {
return "LOOOOOSSEEER";
}
};
alert(compare(userChoice, computerChoice));
}
// startGame();
您在这里使用递归。startGame()函数再次启动,compare()的第一个调用需要一个return语句。但是在这个if子句中,您根本没有返回语句。
// if both selections are the same
if (choice1 == choice2) {
alert("Tie, restart the game!");
startGame();
}
您可以通过if语句中的alert而不是返回结果来解决此问题。这里有一个jsfiddle(函数调用注释):
http://jsfiddle.net/v5poL9cm/
编辑:
另一种方法是在所有情况下都返回消息,并让startGame()函数也有一个return语句。jsfiddle在这里(函数调用注释):
http://jsfiddle.net/65sg7mcL/
$(function () {
startGame();
});
// the game starts here
function startGame() {
// the user
var userChoice = prompt("Do you choose rock, paper or scissors?");
// if invalid input
while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
userChoice = prompt("Please select again, this time correctly!");
}
alert("You chose " + userChoice);
// computer
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
alert("Computer pick: " + computerChoice);
// comparison between user and computer
var result = compare(userChoice, computerChoice);
if (result == "1") {
alert("Tie, restart the game!");
startGame();
} else if (result == "2") {
alert("You have won the game!");
} else {
alert("LOOOOOSSEEER");
}
}
// return 1 -- > Tie
// return 2 -- > You have won
// return 3 -- > LOOOOOSSEEER
function compare(choice1, choice2) {
// if both selections are the same
if (choice1 == choice2) {
return "1";
}
// if the user selects rock
else if (choice1 == "rock") {
if (choice2 == "scissors") {
return "2";
} else {
return "3";
}
}
// if the user selects scissors
else if (choice1 == "scissors") {
if (choice2 == "rock") {
return "2";
}
} else {
return "3";
}
};
我同意@Escobear的观点!问题是,你没有从平局部分返回任何东西,而是重新开始比赛。在某个时刻,它会回到你的脑海中,并试图提醒一个未定义的人。
我想我会这样做:
// comparison between user and computer NOTE: This method is global, but it seemed pointless to redefine it for every game
function compare(choice1, choice2) {
// if both selections are the same
if (choice1 == choice2) {
return -1;
}
// if the user selects rock
else if (choice1 == "rock") {
if (choice2 == "scissors") {
return 1;
}
return 0;
}
// if the user selects scissors
else if (choice1 == "scissors") {
if (choice2 == "rock") {
return 0;
}
return 1;
}
else if (choice1 == "paper") {
if (choice2 == "rock") {
return 1;
}
return 0
}
};
// the game starts here
function startGame() {
// the user
var userChoice = prompt("Do you choose rock, paper or scissors?");
// if invalid input
while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
userChoice = prompt("Please select again, this time correctly!");
}
alert("You chose " + userChoice);
// computer
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
alert("Computer pick: " + computerChoice);
var result = compare(userChoice, computerChoice);
if (result === -1) {
alert('Tie, restart the game!');
startGame();
} else {
alert(result ? 'You have won the game!' : 'LOOOOOSSSEEER!');
}
}
startGame();
(我还纠正了获胜的一些逻辑,并添加了用户输入=纸张)
相关文章:
- 按照选项卡索引的顺序循环一个jQuery选择
- 如何在php中的jquery中对循环内的选择框值求和
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 用于使用javascript循环选择选项
- 循环遍历元素jquery选择器
- 循环选择标记并创建数组或字符串
- 在 while 循环中选择与整数值对应的数组
- 使用循环填充选择
- 使用 for 循环用逗号分隔的值填充选择元素
- 如何在每个循环中选择空输入
- javascript循环选择特定项目
- jquery:如何循环选择多个项目
- Javascript 循环选择所有矩形
- Jquery 使用键向上和向下键循环选择列表项
- jQuery循环选择的最佳方法
- 在Javascript中使用循环选择id的数量
- 通过循环选择复选框
- For循环选择特定的id并更改它们的样式- js
- 为什么我的Javascript循环返回时未定义?如何使用为循环选择对象
- jQuery循环选择并设置所选选项