PDA

Peeyush
05-18-2012, 06:43 AM
heres a sample 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.....

felgall
05-18-2012, 07:37 AM
if (bba==true)

can be abbreviated to

if (bba)

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

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:

ta=' a4 ';
tb = cca ? ' 1x ' : ccb ? ' 2x ' : ccc ? ' 3x ' : ccd ? ' 5x ' : false;
if (tb) versions(ta,tb);}

Peeyush
05-18-2012, 08:27 AM
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);}

can be abbreviated to:

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....

Philip M
05-18-2012, 08:35 AM
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.

felgall
05-18-2012, 09:44 AM
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.

Peeyush
05-18-2012, 12:45 PM
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.

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

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

abduraooft
05-18-2012, 01:02 PM
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.

Peeyush
05-18-2012, 01:38 PM
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....

Logic Ali
05-18-2012, 05:39 PM
If your sample is consistent, you could probably rewrite it something like this:

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);}
}

rnd me
05-18-2012, 09:18 PM
//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()

});