问题描述
我如何使获胜者的代码打印结果和控制台的计数器? 代码的顶部现在可以正常运行。我当前的挑战是代码不会通过if / else条件,并且不会打印到控制台/代码的条件部分似乎没有运行。 我已经尝试过==和===,但没有一个起作用。我错过了什么吗?
//get computer choices
comChoices = ["rock","paper","scissors"];
function playRound() {
//get user input
userInput = prompt("please enter rock,paper or scissors");
//verify its in lowercase
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors"
) {
function comrand() {
// function selects a random computer choice
randomChoice = comChoices[Math.floor(Math.random() *
comChoices.length)];
return randomChoice;
}
window.alert(comrand());
} else {
// veirfy user input
window.alert("please enter rock,paper or scissors or check your
spelling");
playRound();
}
}
//initialize count default
userWin = 0;
comWin = 0;
draw = 0;
//conditionals to determine winners
if (userInput == "rock" && randomChoice == "scissors"){
userWin++;
console.log("You win!!! (rock crushes scissors)");
} else if(userInput == "scissors" && randomChoice =="rock"){
comWin++;
console.log("Computer wins!!!(rock crushes scissors)");
} else if(userInput == "scissors" && randomChoice == "paper"){
userWin++;
console.log("You win!!! (Scissors cuts paper)");
} else if(userInput == "paper" && randomChoice == "scissors"){
comWin++;
console.log("Computer win!s!! (Scissors cuts paper)");
} else if(userInput == "paper" && randomChoice == "rock"){
userWin++;
console.log("You win!!! (paper covers rock)");
} else if(userInput == "rock" && randomChoice == "paper"){
comWin++;
console.log("Computer win!s!! (paper covers rock)");
} else {
draw++;
console.log("game is a draw");
}
if (userWin === 5){ //
conditions to determines the overall winner
console.log("User wins the game");
} else if (comWin === 5){
console.log("computer wins the game");
} else {
console.log("the game is a draw");
}
playRound();
解决方法
我只是颠倒了if条件。很好。
//get computer choices
comChoices = ["rock","paper","scissors"];
//initialize count default
userWin = 0;
comWin = 0;
draw = 0;
function playRound() {
//get user input
userInput = prompt("please enter rock,paper or scissors");
//verify its in lowercase
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors"
) {
randomChoice = comChoices[Math.floor(Math.random() * comChoices.length)];
//conditionals to determine winners
if (userInput == "rock" && randomChoice == "scissors") {
userWin++;
console.log("You win!!! (rock crushes scissors)");
} else if (userInput == "scissors" && randomChoice == "rock") {
comWin++;
console.log("Computer wins!!!(rock crushes scissors)");
} else if (userInput == "scissors" && randomChoice == "paper") {
userWin++;
console.log("You win!!! (Scissors cuts paper)");
} else if (userInput == "paper" && randomChoice == "scissors") {
comWin++;
console.log("Computer win!s!! (Scissors cuts paper)");
} else if (userInput == "paper" && randomChoice == "rock") {
userWin++;
console.log("You win!!! (paper covers rock)");
} else if (userInput == "rock" && randomChoice == "paper") {
comWin++;
console.log("Computer win!s!! (paper covers rock)");
} else {
draw++;
console.log("game is a draw");
}
if (userWin === 5) {
// conditions to determines the overall winner
console.log("User wins the game");
} else if (comWin === 5) {
console.log("computer wins the game");
} else {
console.log("the game is a draw");
}
window.alert(randomChoice);
} else {
// veirfy user input
window.alert("please enter rock,paper or scissors or check your spelling");
}
setTimeout(function () {
playRound();
},1000);
}
playRound();
,
而不是这样做
if(userInput !== "rock" || "paper" || "scissors"){ // veirfy user input
window.alert("please enter rock,paper or scissors or check your spelling");
playRound();
}
执行此操作
if(comChoices.includes(userInput )){
// veirfy user input
} else {
// not verifying user input
}