haeminish
haeminish

Reputation: 1078

if statement checking for multiple boolean conditions

I have a if statement that must call function checkEmpty() for every conditions.

function checkFormFilled(evt){
    var isFilled;
    if(checkEmpty('id1', 'class1') && //condition 1
       checkEmpty('id2', 'class2') && //condition 2
       checkEmpty('id3', 'class3') && //condition 3
       checkEmpty('id4', 'class4')){ //condition 4
       evt.preventDefault();
       isFilled = true;
    }
    return isFilled;
}

The problem is when the condition 1 is false(any preceding condition is false), skips to the evt.preventDefault() line, doesn't get call other following checkEmpty() functions.

I want to call evt.preventDefault() when all conditions return true.

Is there other way around to make this work?

Upvotes: 0

Views: 14873

Answers (7)

NIDHIN VINCENT
NIDHIN VINCENT

Reputation: 235

function checkEmpty(val){ return val;}
function _conditioncheck(e){
    if(checkEmpty(false) && checkEmpty(true) && checkEmpty(true) && checkEmpty(true)){ 
	console.log('true....');
	e.preventDefault();
    }
}	
<input type="button" id="click" value="Click ME" onclick="_conditioncheck(event);" />	

Its working for me.

Upvotes: 2

A. L
A. L

Reputation: 12689

Assuming you have an array of objects, you could try the every function.

Example

var array = [];

for (var i = 0; i < 4; i++)
{
  array.push({
    id: i, bool: true
  });
}

function check(item)
{
  return item.bool == true || false;
}

if (array.every(check))
{
  console.log("all true");
}
else
{
  console.log("return false instead");
}



// Just an example of something that is false
array[1].bool = false;

if (!array.every(check))
{
  console.log("something is false");
}

Upvotes: 1

D Lowther
D Lowther

Reputation: 1619

What you are doing now with the chained && operators is saying if all of these things are true then event.preventDefault. If you really need to check every one of the conditions and proceed if any are true then you should use the logical OR operator || instead.

Upvotes: 0

acdcjunior
acdcjunior

Reputation: 135862

If you

must call function checkEmpty() for every conditions.

And

I want to call evt.preventDefault() when all conditions return true.

If you are sure that checkEmpty() returns a boolean, you could use the bitwise and (&) operator:

function checkEmpty(x) {
  console.log('checkEmpty called for ' + x);

  return false;
}
if(checkEmpty('..1', '....') & //condition 1
   checkEmpty('..2', '....') & //condition 2
   checkEmpty('..3', '....') & //condition 3
   checkEmpty('..4', '....')){ //condition 4
  console.log('inside if');
}

Output:

checkEmpty called for ..1
checkEmpty called for ..2
checkEmpty called for ..3
checkEmpty called for ..4

See fiddle demo here.

Upvotes: 1

behkod
behkod

Reputation: 2777

Its called short-circuit.

1- In a condition check composed of &&, if the first element evaluates to false, all of the remaining conditions are ignored, returning false for the whole condition.

2- In a condition check composed of ||, if the first element evaluates to true, all of the remaining conditions are ignored, returning true for the whole condition.


He edited the question. this is no more true. I'm making it community wiki, here to stay.

Best workaround to this is using || or re-ordering sequence of sub-conditions in your condition-check to let other elements get tested whenever you need.

Upvotes: 1

Brian Destura
Brian Destura

Reputation: 12078

If at least one of the conditions is False, it will not go inside the IF block. For multiple && statements, once a FALSE is received, all other succeeding && statements are not checked anymore and FALSE is returned.

Upvotes: 1

Nicolas
Nicolas

Reputation: 8680

try the logical operator || which is OR

Upvotes: 4

Related Questions