www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] A more elegent way of checking if a whole bunch of if statements are true

  1. #1
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88

    resolved [RESOLVED] A more elegent way of checking if a whole bunch of if statements are true

    Hi, I was just wondering if there was a more elegent way of checking if a whole bunch of if statements are true...
    Code:
         
        var unityObjectValue1 = "n", unityObjectValue2 = "i", unityObjectValue3 = "space", unityObjectValue4 = "reg", unityObjectValue5 = "a", unityObjectValue6 = "s", unityObjectValue7 = "space", unityObjectValue8 = "la", unityObjectValue9 = "space", unityObjectValue10 = "homar", unityObjectValue11 = "o", unityObjectValue12 = "n"; 
        
        // this part...
        if( unityObjectValue1 == "n" && 
            unityObjectValue2 == "i" && 
            unityObjectValue3 == "space" && 
            unityObjectValue4 == "reg" && 
            unityObjectValue5 == "a" && 
            unityObjectValue6 == "s" && 
            unityObjectValue7 == "space" && 
            unityObjectValue8 == "la" && 
            unityObjectValue9 == "space" && 
            unityObjectValue10 == "homar" &&
            unityObjectValue11 == "o" && 
            unityObjectValue12 == "n")
        {
            // destroy the mini game trigger object. 
            // exit the mini game. 
            // run the animation. 
            console.log("Matched.");
        } 
        else 
        {
            console.log("Not Matched.");
        }
    Happy Yule

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,432

    Lightbulb

    Have you considered something like this?
    Code:
    <script type="text/javascript">
      var unityObjectValue = ['n','i','space','reg','a','s','space','la','space','homar','o','n'];  // original
      var userValueOK      = ['n','i','space','reg','a','s','space','la','space','homar','o','n'];  // duplicate
      var userValueNotOK   = ['a','b','space','reg','c','d','space','la','space','homar','e','f'];  // not same
      
    function checkUnity(O,T) {
      var flag = true;
      for (var i=0; i<O.length; i++) {
        if (O[i] != T[i]) { flag = false; }
      }
      return flag;
    }
      if (checkUnity(unityObjectValue,userValueOK)) { console.log('Matched.'); } else { console.log('Not Matched'); }
      if (checkUnity(unityObjectValue,userValueNotOK)) { console.log('Matched.'); } else { console.log('Not Matched'); }
    </script>

  3. #3
    Join Date
    Dec 2013
    Posts
    21
    Your method is not bad, it is just that in general, it is better not to use 'magic' values within the function code itself. JMRKER showed how you could define the values in an array where it is easily maintainable.

    Since your checks all involve strings, could you just concatenate all of the input values and check against a master string? ie:

    var master_string = "nispacereg.....etc"
    var input_string = unityObjectValue1 + unityObjectValue2 + unityObjectValue3...etc
    //note: you could also do the concatenation in a loop

    then compare: if(input_string == masterstring)

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,498
    what would help is if unityObjectValue was an array of nth values and then your code would look something like

    Code:
    var checks = ["n","i","space","reg","a","s","space","la","space","homar","o","n"];
    for(c=0,testVar=false;c<checks.length;c++)
        testVar = unityObjectValue[c] == checks[c]? true : testVar;
    
        if( testVar ){ 
            // we are true, do we do something?
       }else{
            // were false, do something?
            }
    which is useful if you want to set a flag then do something if tru value has been reached.

    you may want to use something different like
    Code:
    var checks = ["n","i","space","reg","a","s","space","la","space","homar","o","n"];
    for(c=0;c<checks.length;c++)
        if( unityObjectValue[c] == checks[c]){ 
            // we are true, do we do something?
           ... 
           ...
       }
    if you want to do something when you have a matched value

    you can easily combine the two...

    Code:
    var checks = ["n","i","space","reg","a","s","space","la","space","homar","o","n"];
    for(c=0,testVar=false;c<checks.length;c++){
        testVar = unityObjectValue[c] == checks[c]? true : testVar;
    
        if( unityObjectValue[c] == checks[c] ){
           // we have a match
           ...
        }
    }
    
    // house keeping, do somthing if we have a match
    if( testVar ){ 
          // we are true, do we do something?
          }else{
          // were false, do something?
          }
    Your choice of methods depends on your needs, going with an array and iterating it is IMHO far better than having lots of variables of a similar name.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

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

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