#javascript #logical-operators
#javascript #логические операторы
Вопрос:
Как мне заставить код выводить результаты winner и счетчик на консоль? Верхняя часть кода теперь работает нормально. моя текущая проблема заключается в том, что код не проходит через условие 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" amp;amp; randomChoice == "scissors"){
userWin ;
console.log("You win!!! (rock crushes scissors)");
} else if(userInput == "scissors" amp;amp; randomChoice =="rock"){
comWin ;
console.log("Computer wins!!!(rock crushes scissors)");
} else if(userInput == "scissors" amp;amp; randomChoice == "paper"){
userWin ;
console.log("You win!!! (Scissors cuts paper)");
} else if(userInput == "paper" amp;amp; randomChoice == "scissors"){
comWin ;
console.log("Computer win!s!! (Scissors cuts paper)");
} else if(userInput == "paper" amp;amp; randomChoice == "rock"){
userWin ;
console.log("You win!!! (paper covers rock)");
} else if(userInput == "rock" amp;amp; 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();
Комментарии:
1. Добро пожаловать! Для начала,
(userInput !== "rock" || "paper" || "scissors")
всегда будет true, потому"paper"
что и"scissors"
являются правдивыми. Вы могли бы использовать(userInput !== "rock" || userInput !== "paper" || userInput !== "scissors")
, или(!comChoices.includes(userInput))
.2. Я бы создал три кнопки…
Ответ №1:
Вместо этого
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
}
Ответ №2:
Я только что изменил условие 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" amp;amp; randomChoice == "scissors") {
userWin ;
console.log("You win!!! (rock crushes scissors)");
} else if (userInput == "scissors" amp;amp; randomChoice == "rock") {
comWin ;
console.log("Computer wins!!!(rock crushes scissors)");
} else if (userInput == "scissors" amp;amp; randomChoice == "paper") {
userWin ;
console.log("You win!!! (Scissors cuts paper)");
} else if (userInput == "paper" amp;amp; randomChoice == "scissors") {
comWin ;
console.log("Computer win!s!! (Scissors cuts paper)");
} else if (userInput == "paper" amp;amp; randomChoice == "rock") {
userWin ;
console.log("You win!!! (paper covers rock)");
} else if (userInput == "rock" amp;amp; 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();
Комментарии:
1. вау, спасибо, это сработало. я добавил вторую часть кода, которая не будет выполнять условные выражения. пожалуйста, помогите мне взглянуть. он не выводит результат и счетчик на консоль.
2. @Michael теперь проверьте это.
3. большое вам спасибо. теперь я понимаю, что я пропустил и почему. ты потрясающий!!!