Javascript如果条件与多个OR和AND [英] Javascript If Condition with multiple OR and AND

查看:563
本文介绍了Javascript如果条件与多个OR和AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看我的情况。
我只是刷新我的JavaScript,我想知道如何检查,如果插入的变量是我想要使用的。



显然如果(选择1和选择2等同于石头或剪刀或纸){$

现在if条件说明,如果b $ b做到这一点;
} else {做别的事情}



但显然它不符合我的要求。

  var choice1 = prompt(Player 1:Rock,scissors or paper?); 
var choice2 =提示(玩家2:摇滚,剪刀或纸?);

比较(choice1,choice2);

函数比较(choice1,choice2){

if(choice1&& choice2 ===rock||paper|| scissors){
alert(你通过);
} else {
alert(出错了);
}

任何人都可以给我一个简短的解释,为什么if条件通过每个值获得?它永远不会显示消息出错了。

解决方案

我相信它看起来像:

  if((choice1 ==rock|| choice1 ==paper|| choice1 ==scissors)&& 
(choice2 ==rock|| choice2 ==paper|| choice2 ==scissors)){...


please have a look on my if condition. I am just refreshing my javascript and I am wondering, how I could check, if the inserted variables, are the ones I want to be used.

Obviously the "game" should only take rock, paper or scissors.

Now the if condition says, if (choice 1 and choice 2 equal rock or scissors or paper) { do this; } else {do something else}

But apparently it is not working the way I want.

var choice1 = prompt("Player 1: Rock, scissors or paper?");
var choice2 = prompt("Player 2: Rock, scissors or paper?");

compare(choice1, choice2);

function compare(choice1, choice2) {

if(choice1 && choice2 === "rock" || "paper" || scissors) {
    alert("You pass");
} else {
    alert("Something went wrong");
}

Could anyone give me a short explanation, why the if condition passes every value it gets? It never shows the mssage "Something went wrong".

解决方案

I believe it shoud look like:

if ((choice1=="rock" || choice1=="paper" || choice1=="scissors") && 
    (choice2=="rock" || choice2=="paper" || choice2=="scissors")){...

这篇关于Javascript如果条件与多个OR和AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆