Javascript “if” with “or” How check which will exec

Multi tool use
Multi tool use


Javascript “if” with “or” How check which will exec


if (a != 1 || b != 2) {
console.log(this, ' is required');
}



Can we get the answer a is required or b is required?


a is required


b is required





Why not have two different ifs?
– Mureinik
Jul 1 at 17:56





You need to use multiple if statements.
– Get Off My Lawn
Jul 1 at 17:56





because I want check many parameter 10 or 20 - and i dont want 10 ifs
– Alex Latro
Jul 1 at 17:57




4 Answers
4



I am assuming this is what you want. If not, add more information.


function checkVariable(variable, value){
if(variable!=value) return variable+" is required";
}



There's no way to get the answer from variable that executed but I have another way to do.


var array = [{name:"a",value:2,checkvalue:1},{name:"b",value:3,checkvalue:2}];

for(var i = 0 ; i < array.length ; i++){
if(array[i].value!=array[i].checkvalue){
console.log(array[i].name + "is required!");
}
}



try this. I hope it can be adapted.



This would probably work, too:




const a = document.getElementById('a')
const b = document.getElementById('b')
let c;

function evaluateAB() {
c = "";
if((Number(a.value) != 1 && (c="a")) || (Number(b.value) != 2 && (c="b"))){
console.log(c ? c + ' is required' : void(0));
}
}


<label>a: <input type="number" value="0" id="a" /></label>
<label>b: <input type="number" value="0" id="b" /></label>
<button type="button" onclick="evaluateAB()">Evaluate</button>





How will that look for 20 variables, which OP commented there might be?
– LGSon
Jul 1 at 18:18






@LGSon Ugly... very ugly. Just hoping this gives some inspiration.
– connexo
Jul 1 at 18:26





Right....hahaha :)
– LGSon
Jul 1 at 18:27



When javascript short circuit that expression, it won't tell where, so you need to go over them, one-by-one.



If you have that many variables, I recommend to use an object instead, which would make it all so much easier, and you avoid pollute the environment with a bunch of variables.



When done like this, it is easy to expand, by simply add a new key/value pair, and pass cross different methods etc.



Stack snippet




// setup and init an object
var my_vars = {
"a": 0,
"b": 0,
"c": 0,
"d": 0
}

// assign its property values somewhere in your code
my_vars["a"] = 1;
my_vars["b"] = 2;
my_vars["d"] = 4;


// check which is missing
for (v in my_vars) {
if (my_vars[v] == 0) {
console.log(v, 'is required' );
}
}





He might use a set as well.
– connexo
Jul 1 at 18:30





@connexo Of course
– LGSon
Jul 1 at 18:34





Also note that he's checking a to not be 1, and b to not be 2.
– connexo
Jul 1 at 18:36


a


b





@connexo Yeah, seen that, though assume those values are dummies. We'll see what response we get.
– LGSon
Jul 1 at 18:41







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

uBeJ3,20 kf,08aWcvdf6s,yK,3jWeFH erpWaw xbY10lJx,c8igOjx6G IfoG X,Lx,lCa XTQ ei sE,5,Uss,LXLaJ0KIfj 9
m,Z0hFsatxNwUcttkW,y

Popular posts from this blog

Rothschild family

Cinema of Italy