这里有些东西:
-
在另一个函数中定义的某些函数,因此不能调用它们
-
我删除了所有那些不必要的参数和返回语句
-
我定义了
input
在
startGame()
使其在内部可访问的函数
checkInput()
--通过
输入
作为一个参数也可以
-
我还移除了无效和不必要的while循环
开始时间()
-
开始时间()
在里面
检查输入()
所以没人叫它,所以比赛就没开始
//Is gameOver defined correctly? I'm not sure.
let gameOver = false;
let input;
let computerChoices = ["rock", "paper", "scissors"];
let computerChoice = randomFrom(computerChoices);
console.log(computerChoice);
// //change Math.ceil to Math.floor in order to include the [0] index
function randomFrom(array) {
return array[(Math.floor(Math.random() * 3))];
}
//Changed computerChoices to computerChoice
function startGame() {
//Added input in order to change user input to lowercase characters
input = prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.").toLowerCase();
checkInput();
}
//Changed computerChoices to computerChoice
function checkInput() {
if (input === "quit") {
return;
}
if (computerChoice === "rock" && input === "scissors") {
alert("Computer wins!");
} else if (computerChoice === "scissors" && input === "paper") {
alert("Computer wins!");
} else if (computerChoice === "paper" && input === "rock") {
alert("Computer wins!");
//Added user outcomes since program results in the computer always winning
} else if (computerChoice === "rock" && input === "paper") {
alert("Player wins!");
} else if (computerChoice === "scissors" && input === "rock") {
alert("Player wins!");
} else if (computerChoice === "paper" && input === "scissors") {
alert("Computer wins!");
} else if (computerChoice === input) {
alert("we have a draw");
} else {
console.log("error");
}
}
//I'm calling the startGame() function in order to get it to run
startGame();
记住,如果你想比赛持续的时间超过一轮,你需要一个循环。
其次,这里是一个工作示例,因此决定显示一些错误,我在测试堆栈片段以外的任何地方都没有遇到这些错误:
http://jsfiddle.net/zvao25ny/1/