Javascript 如何使用or(| |)逻辑运算符验证用户输入id(石头、布或剪刀)?
如何将winner和计数器的代码打印结果发送到控制台? 代码的顶部现在运行良好。我目前面临的挑战是,代码没有通过if/else条件,也没有打印到控制台/代码的条件部分似乎没有运行。 我已经试过==和===了,但都不起作用。我错过什么了吗Javascript 如何使用or(| |)逻辑运算符验证用户输入id(石头、布或剪刀)?,javascript,logical-operators,Javascript,Logical Operators,如何将winner和计数器的代码打印结果发送到控制台? 代码的顶部现在运行良好。我目前面临的挑战是,代码没有通过if/else条件,也没有打印到控制台/代码的条件部分似乎没有运行。 我已经试过==和===了,但都不起作用。我错过什么了吗 //get computer choices comChoices = ["rock", "paper", "scissors"]; function playRound()
//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条件。它很好用
//获取计算机选项
comChoices=[“石头”、“布”、“剪刀”];
//初始化默认计数
userWin=0;
comWin=0;
绘图=0;
函数playRound(){
//获取用户输入
用户输入=提示(“请输入石头、布或剪刀”);
//验证其是否为小写
userInput=userInput.toLowerCase();
如果(
用户输入==“摇滚乐”||
用户输入==“纸张”||
用户输入==“剪刀”
) {
randomChoice=comChoices[Math.floor(Math.random()*comChoices.length)];
//决定胜利者的条件
if(userInput==“rock”&&randomChoice==“剪刀”){
userWin++;
log(“你赢了!!!(石头压碎剪刀)”;
}else if(userInput==“剪刀”&&randomChoice==“石头”){
comWin++;
log(“电脑赢!!!(石头压碎剪刀)”;
}else if(userInput==“剪刀”&&randomChoice==“纸”){
userWin++;
log(“你赢了!!!(剪刀剪纸)”;
}else if(userInput==“paper”&&randomChoice==“剪刀”){
comWin++;
log(“电脑赢了!s!!(剪刀剪纸)”;
}else if(userInput==“paper”&&randomChoice==“rock”){
userWin++;
log(“你赢了!!!(纸盖石头)”);
}else if(userInput==“rock”&&randomChoice==“paper”){
comWin++;
log(“computerwin!s!!(纸盖石头)”;
}否则{
draw++;
log(“游戏是平局”);
}
如果(userWin==5){
//决定总冠军的条件
log(“用户赢得游戏”);
}否则如果(comWin==5){
log(“计算机赢得游戏”);
}否则{
log(“游戏是平局”);
}
窗口。警报(随机选择);
}否则{
//谨慎的用户输入
窗口提示(“请输入石头、布或剪刀或检查拼写”);
}
setTimeout(函数(){
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
}
欢迎对于初学者来说,(userInput!=“rock”| |“paper”| |“剪刀”)
总是正确的,因为“paper”
和“剪刀”
都是真实的。你可以使用(userInput!==“rock”| | userInput!==“paper”| | userInput!==“剪刀”)
,或者(!comChoices.includes(userInput))
。我会创建三个按钮……哇,谢谢,效果不错。我添加了代码的第二部分,它不会运行条件。请帮我看一下。它不会打印结果,也不会计数器到控制台。@Michael,现在检查一下。非常感谢。我现在明白了我错过了什么以及为什么。你太棒了!!!