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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts

    any JS shorthand property for if ? how to compress a code with lot of 'if' statements

    heres a sample code:
    Code:
    	if (bba==true)
    		{
    			ta=' a4 ';
    			if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
    			if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
    			if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
    			if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    		}
    	if (bbb==true)
    		{
    			ta=' a3 ';
    			if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
    			if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
    			if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
    			if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    		}
    	if (bbc==true)
    		{
    			ta=' a2 ';
    			if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
    			if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
    			if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
    			if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    		}
    any way i could make it shorter? this is just sample code, i dont know, but i may have to write 70/3 times this code.....
    Everything is simpler with jQuery!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    if (bba==true)
    can be abbreviated to

    Code:
    if (bba)
    since the content of the () simply needs to be true in order for the following statement to run.

    Code:
    ta=' a4 ';
    if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
    if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
    if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
    if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    can be abbreviated to:

    Code:
    ta=' a4 ';
    tb = cca ? ' 1x '  :  ccb ? ' 2x ' : ccc ? ' 3x '  : ccd ? ' 5x ' : false;
    if (tb) versions(ta,tb);}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Peeyush (05-18-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by felgall View Post
    Code:
    if (bba==true)
    Code:
    ta=' a4 ';
    if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
    if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
    if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
    if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    can be abbreviated to:

    Code:
    ta=' a4 ';
    tb = cca ? ' 1x '  :  ccb ? ' 2x ' : ccc ? ' 3x '  : ccd ? ' 5x ' : false;
    if (tb) versions(ta,tb);}
    those arnt if, else if, else statements..... so i dont think conditional oprator shall be used here....
    Everything is simpler with jQuery!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Peeyush View Post
    those arnt if, else if, else statements..... so i dont think conditional oprator shall be used here....
    Peeyush - felgall is one of the most experienced and respected coders around. It is verging on insulting to suggest that his advice to you is incorrect. You really must try to be a little less precocious.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Peeyush (05-18-2012)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Peeyush View Post
    those arnt if, else if, else statements
    despite appearances to the contrary those are in fact a way of specifying if else if else ... where all of the code is setting the same variable. You can only code it that way when all of the conditions are setting different values in the same variable.

    See http://javascriptexample.net/basics13.php for my explanation of this along with an actual working example.

    Anyway if you try the code I posted you will find it does exactly the same thing as the if else chain you were asking about.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Peeyush (05-18-2012)

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Hey i apologize to felgall and any other member of this community who might have been affected by my behavior. in future i shall use better language to express my views.

    now back to topic,

    Code:
    ta=' a4 ';
    tb = cca ? ' 1x '  :  ccb ? ' 2x ' : ccc ? ' 3x '  : ccd ? ' 5x ' : false;
    if (tb) {versions(ta,tb);}
    didnt work as i wanted. what i want the code to do is
    Code:
    			if (cca==true){ tb = ' 1x ' ;versions();}
    			if (ccb==true){ tb = ' 2x ' ;versions();}
    			if (ccc==true){ tb = ' 3x ' ;versions();}
    			if (ccd==true){ tb = ' 5x ' ;versions();}
    first the code will check if cca is true, if it is then code sets tb to 1x and calls versions(); , when it returns it checks other conditions too. which means versions shall be called many number of times depending on how many checkboxes are checked (cca,ccb,ccc and ccd represent state of checkbox) each time with value of tb as different... and thanks for your first code, it proved to be useful
    Everything is simpler with jQuery!

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    versions shall be called many number of times depending on how many checkboxes are checked (cca,ccb,ccc and ccd represent state of checkbox) each time with value of tb as different.
    Please post your complete code including HTML. There must be some simple way to remove those repeating if conditions by the use of a loop.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    well the whole code will really waste a lot of time of you guys. i dont know how to explain this , its kinda complicated. i have am writing and deleting from past 30 mins. i think i can live with 60 if statements as now felgall has helped me make 'em shorter....
    Everything is simpler with jQuery!

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    If your sample is consistent, you could probably rewrite it something like this:
    Code:
    var data =[ bba, ' a4 ', bbb, ' a3 ', bbc, ' a2 ' ];
    
    for( var i = 0, flag = false; i < data.length; i += 2 )
      if( data[ i ] )
      {
        ta = data[ i + 1 ];
        flag = true
      }
    
    if( flag )
    {
       if (cca==true){ tb = ' 1x ' ;versions(ta,tb);}
       if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);}
       if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);}
       if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);}
    }

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,375
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Code:
    //list the changing As in order of bbx:
    var prop=[" a4 ", " a3 ", " a2 "];
    
    //bbx:   
     [bba, bbb, bbc].filter( 
          Boolean   // ==if (bbc==true)
      ).map(function( x, i){
    
       ta=prop[i]; //==  ta=' a2 ';
    
      
      [cca, ccb, ccc, ccd].map(function(a,b){
         if(!a){return;}   //== all those IFs...
          tb= " "+b+1+" ";  // ==  tb = ' 1x ' 
          versions(ta,tb);    // == versions(ta,tb);
      })//end map()
     
    });
    Last edited by rnd me; 05-18-2012 at 09:24 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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