Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Adding values of array to IF condition

    I am attempting to create a function that follows similar logic to the following:
    Code:
    function ___ (array1, array2) {
      if( for (i in array1) {
          $("#"+array1[i]).val() != ""}){
        for (i in array2) {
          $("#"+array1[i]).show()}
        }
    }
    I want to feed an array of values into a function, every value in the array must act as an extra condition in an IF statement.
    Is it possible?

    My best attempt was the next chunk of code which fell flat.
    I attempted to create a string representing the condition, but the IF treats the var as the condition( therefore testing its existence), not the value it represents.

    Code:
    $(document).ready(function(e) {
    	var q = ['fname', 'lname'];
    	
    	_____(q, "");
    });
    
    function _____(q, a){
    	
    	// Initialize the condition
    	var condition = "";
    	// Create a count for conditions
    	var count = 0;
    	// Loop through array containing  IDs to be included in the condition
    	for ( i in q ) {
    		// If the condition is not the first, add &&
    		if(count == 0) {
    			condition += '$("#' + q[i] + '").val() != "" ';
    		} else {
    			condition += '&& $("#' + q[i] + '").val() != ""';
    		}
    		// Increment count
    		count++;
    	}
    }
    I apologise if the above isnt as clear as it should be.

    *There is jQuery included in that code

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Maybe like this?

    Code:
    function multipleCondition(myArray, myValue) {
       var returnValue = true;
       for(i=0; i<myArray.length; i++) {
          if(myArray[i] == myValue) returnValue = false;
       }
       return returnValue;
    }
    
    // example
    if(multipleCondition(["fname", "lname"], "")) {
       // all of the array conditions are true
    }
    The function will return "true" only if all of the array elements are != to the myValue parameter. If one of the elements is equal to myValue, it will return false and the whole multiple condition fails.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    isn’t that the same as
    PHP Code:
    if (-=== myArray.indexOf(myValue)) {
         
    // myValue not in myArray

    ?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Yes you are right ... I forgot to include the jQuery part
    Code:
    function multipleCondition(myArray, myValue) {
       var returnValue = true;
       for(i=0; i<myArray.length; i++) {
          if($('#'+myArray[i]).val() == myValue) returnValue = false;
       }
       return returnValue;
    }
    
    // example
    if(multipleCondition(["fname", "lname"], "")) {
       // all of the array conditions are true
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •