0

I have an instrestion question about boolean logiс in javascript.

I have a FOR circle and created a trigger event, which will store TRUE or FALSE for IF statement:

var trigger = [];

for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){

        bla bla bla

        trigger.push(true)
        }

        else {

        trigger.push(false);
        }
    }

So in the end I want to check each statement of array for boolean operator AND

if(trigger & ??? )

Any help friends ?

7
  • 2
    I'm not sure if I understand: Do you want to count how often true is in the array or test whether all entries are true? What else are you doing with trigger? Do you need an array at all? & is bitwise AND btw. Commented Mar 3, 2012 at 12:20
  • Man, i want to check by boolean logic if the statement will be correct ('111' & '111') will return TRUE Commented Mar 3, 2012 at 12:22
  • Still not clear and '111' & '111' actually returns 111. I honestly suggest you to take some more time to phrase your question properly. It is not easy to understand what you want to do (or maybe it's just me who does not understand it). Commented Mar 3, 2012 at 12:23
  • boolean operator and is && .. if you want to check if 2 variables have the same value you can use == or === for both value and type (strict comparison) checks Commented Mar 3, 2012 at 12:24
  • 1
    Masks are numbers though, not arrays. Why don't you just increase a counter instead of adding true to the array and later test whether $counter === 3? Commented Mar 3, 2012 at 12:30

2 Answers 2

5

Based on one of your comments

Ok, how i can check each statement in array if it's TRUE, than return true

Like this, for example:

var alltrue = !/false/i.test(trigger.join(''));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man -i've used your solution, but i think it'll be buggy in different browsers
1

(I've marked new lines of code with //@@@) I assume you want to check if each statement is true. Then, there are two ways, depending on if the "bla bla bla" is actually there.

If the forloop is only for checking if every field is empty (i.e. the "bla bla" is empty), and the trigger array isn't being used afterwards, your if statement can be compactly written as:

var trigger = [];
var empty=false; //@@@
for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){
        }

        else {
        empty=true; //@@@
        break; //@@@
        }
    }
}

If you want to be able to use trigger afterwards, do this:

var trigger = [];
var dummy=[];  //@@@@
for (i = 0; i < 3; i++ ){

    //Checking for empty fields
    if(this.form.rows[i].fields[0].input.val() === '') {
        if($(this.form.rows[i].row[0]).length){

        bla bla bla

        trigger.push(true)
        }

        else {

        trigger.push(false);
        }

        dummy.push(true);   //@@@@
    }

//Use this if block:
if(dummy.toString()=trigger.toString()){
   //Insert stuff here
}
//Alternatively,  use this:

if(dummy && trigger){
   //Insert stuff here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.