www.webdeveloper.com
Results 1 to 6 of 6

Thread: Loop through variables in a funtion

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    Loop through variables in a funtion

    Hello

    I'm a newb to Javascript to this should be an easy one

    I have a function with 4 variables that i want to loop through to determine if at least 1 exists. If at least 1 exists (or more) i want to return true, if none exist i want to return false. I know how to do it for just 1 variable but how can i do it with 4 in this situation? Thanks!

    function seg_exists ()
    {

    var a = sega
    var b = segb
    var c = segc
    var d = segd

    if (a == undefined || a.toString().length == 0)
    { return false}

    return true
    }

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you can't loop plain old vars...

    since you are eliminating "", i'll go out an a limb and guess you don't care about false (as a Boolean) or 0 (as a Number) either, in which case it's simple:


    Code:
    function seg_exists (){
    
    var a = sega
    var b = segb
    var c = segc
    var d = segd
      return a && b && c && d;
    }

    to catch 0 and false:
    Code:
    function seg_exists (){
     function ok(v){  return v || ({false:1, "0":1 })[v] ? v : undefined; }
    var a = sega
    var b = segb
    var c = segc
    var d = segd
      return ok(a) && ok(b) && ok(c) && ok(d);
    }

  3. #3
    Join Date
    Oct 2009
    Posts
    6
    Thank you very much for the reply. The examples didn't work so perhaps i need to explain a little better.

    I have 4 variables that i want to look through to see if at least 1 exists. The variables represent fields with text inside. If none of the fields exist, or if the fields exist but do not contain any text, i want to return false. If at least 1 field exists with text inside i want to return true. The example that i gave before works fine when only 1 variable exists. I just don't know what to do when i have to look at 4 to determine if at least 1 exists.
    Thanks

  4. #4
    Join Date
    Oct 2009
    Posts
    6
    OK, i have it working now, although i don't think it's the best way to do it.

    function seg_exists ()
    {

    var a = sega
    var b = segb
    var c = segc
    var d = segd

    if (a.toString().length == 0 && b.toString().length == 0 && c.toString().length == 0 && d.toString().length == 0)
    { return false}

    return true
    }

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    Why don't you use an array and loop through them? You can return true once one of them is set and not empty and return false outside the loop since none of them met your condition.

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by tranquil View Post
    Thank you very much for the reply. The examples didn't work so perhaps i need to explain a little better.

    I have 4 variables that i want to look through to see if at least 1 exists. The variables represent fields with text inside. If none of the fields exist, or if the fields exist but do not contain any text, i want to return false. If at least 1 field exists with text inside i want to return true. The example that i gave before works fine when only 1 variable exists. I just don't know what to do when i have to look at 4 to determine if at least 1 exists.
    Thanks

    i think that's basically what the code i gave you does:
    Code:
    var sega="a",
    segb="b",
    segc="c",
    segd="d";
    
    function seg_exists (){
     function ok(v){  return v || ({false:1, "0":1 })[v] ? v : undefined; }
    var a = sega
    var b = segb
    var c = segc
    var d = segd
      return ok(a) && ok(b) && ok(c) && ok(d)  && true;
    }
    
    alert(seg_exists())//===true
    i didn't realize you need it specifically true, but since the strings have to be non-empty to return true, it should have worked just fine.

    i added a forced Boolean return for you, just in case you have something like if(seg_exists()===false) in your code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles