![](/img/trans.png)
[英]What code do I need add so that this javascript adds 3 dots at the end of the text automatically?
[英]What do i need to add, so my JavaScript would work as i want it to?
我开始学习JavaScript ,这是我构建的第一款游戏:Rock,Paper,Scissors。 (玩家与电脑)现在,一切正常,但我无法弄清楚如何做到这一点 :
当结果为平局时,游戏再次开始询问我是否想再次播放,而无需刷新浏览器。
请不要用jQuery回答 ,我正在尝试学习JavaScript! :)
这是我的片段:
// declare variables var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); // prevent the user from choosing a different answer if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") { userChoice = prompt("Your answer is not acceptable! Please choose again!"); } // pick a random answer from computer if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } alert("Computer: " + computerChoice); // check who wins var compare = function(choice1, choice2) { if (choice1 === choice2) { alert("The result is a tie!"); } else if (choice1 === "rock") { if (choice2 === "scissors") { alert("rock wins"); } else { alert("paper wins"); } } else if (choice1 === "paper") { if (choice2 === "rock") { alert("paper wins"); } else { alert("scissors win"); } } } // call the function compare(userChoice, computerChoice);
你基本上要做的是将游戏包装在一个更大的上下文循环中,它本身只是检查游戏的终止条件。
在结构上,让我们假设您的整个游戏都包含在一个函数中:
function playGame() {
// all the code you have now
}
然后循环将有这样的结构:
while (true) {
playGame();
if (!confirm("Would you like to play again?")) {
break;
}
}
当然,你可以通过确认来获得更好的功能,或者使用一个值来控制循环而不是一个带有break
的无限循环。 但这个概念是一样的。 整个循环将继续重复并“再次播放”,直到存在某些条件导致其停止。
所以在你的情况下,你想检查它是否是平局,是吗? 结构上是这样的:
while (true) {
playGame();
if (!gameIsTie()) {
break;
}
}
要么:
var winner = 0;
while (winner == 0) {
playGame();
winner = determineWinner();
}
其中你将实现确定游戏是否平局所需的功能,或者是否有赢家等。
如果是平局,你可以把所有功能和呼叫都放在一起。
(function game(){ // declare variables var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); // prevent the user from choosing a different answer if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") { userChoice = prompt("Your answer is not acceptable! Please choose again!"); } // pick a random answer from computer if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } alert("Computer: " + computerChoice); // check who wins var compare = function(choice1, choice2) { if (choice1 === choice2) { alert("The result is a tie!"); var newGame = prompt("Play again?"); if(newGame === 'yes'){ game(); } } else if (choice1 === "rock") { if (choice2 === "scissors") { alert("rock wins"); } else { alert("paper wins"); } } else if (choice1 === "paper") { if (choice2 === "rock") { alert("paper wins"); } else { alert("scissors win"); } } } // call the function compare(userChoice, computerChoice); })()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.