getHumanChoice()
您已经定义了该函数
playGame()
,但是您从未称呼它。因此,为什么不进行用户输入或显示任何控制台消息。
此外,为了使内容更具可读性,请单独定义所有功能,不要在其他功能中定义它们,尤其是在这种情况下有三个功能时。这是不好的,因为每次调用function playGame() {
var humanScore = 0;
var computerScore = 0;
for (let i = 0; i < 5; i++) {
function getComputerChoice() {
let result = Math.ceil(Math.random() * 3);
if (result === 1) {
return "rock";
} else if (result === 2) {
return "paper";
} else {
return "scissors"
}
}
const computerChoice = getComputerChoice();
function getHumanChoice() {
let humanResult = prompt("Let's play Rock, Paper, Scissors! Enter your choice:");
if (humanResult.toLowerCase() === "rock") {
return "rock";
} else if (humanResult.toLowerCase() === "paper") {
return "paper";
} else if (humanResult.toLowerCase() === "scissors") {
return "scissors";
} else {
return "to not follow the rules";
}
};
const humanChoice = getHumanChoice();
function playRound(humanChoice, computerChoice) {
if (humanChoice === "rock" && computerChoice === "paper") {
computerScore++;
console.log(`Paper beats rock! AI wins! AI:${computerScore} Human:${humanScore}`);
} else if (humanChoice === "rock" && computerChoice === "scissors") {
humanScore++;
console.log(`Rock beats scissors! You win! AI:${computerScore} Human:${humanScore}`);
} else if (humanChoice == "rock" && computerChoice === "rock") {
console.log("Y'all both picked rock! Let's try again!");
} else if (humanChoice === "paper" && computerChoice === "scissors") {
computerScore++;
console.log(`Scissors beats paper! AI wins! AI:${computerScore} Human:${humanScore}`);
} else if (humanChoice === "paper" && computerChoice === "rock") {
humanScore++;
console.log(`Paper beats rock! You win! AI:${computerScore} Human:${humanScore}`);
} else if (humanChoice === "paper" && computerChoice === "paper") {
console.log("Y'all both picked paper! Let's try again!");
} else if (humanChoice === "scissors" && computerChoice === "scissors") {
console.log("Y'all both picked scissors! Let's try again!");
} else if (humanChoice === "scissors" && computerChoice === "rock") {
computerScore++;
console.log(`Rock beats scissors! AI wins! AI:${computerScore} Human:${humanScore}`);
} else if (humanChoice === "scissors" && computerChoice === "paper") {
humanScore++;
console.log(`Scissors beats paper! You win! AI:${computerScore} Human:${humanScore}`);
} else {
console.log("Try again! That's not a choice.");
}
}
}
};
playGame();
playRound()
playGame()
依赖于变量playGame()
playGame()