...

View Full Version : Resolved javascript code producing unexpected results, a ques related to Conditional Operator.



Peeyush
05-17-2012, 12:22 PM
this is my javascript code:


var aaa, aab;
aaa = choo.chooserver0.checked;
aab = choo.chooserver1.checked;
k=(aaa==true)?0:(aab==true)?1:(aaa=true&&aab==true)? 'b2' : false ;


now when the checkbox with id chooserver0 is checked, it sets k's value to 0, and when checkbox with id chooserver1 is checked it sets its value to 1, but when both are checked it must set value to b2, but it sets value to 0, why?

and when i write it as:



var aaa, aab;
aaa = choo.chooserver0.checked;
aab = choo.chooserver1.checked;
k=(aaa=true&&aab==true)? 'b2' :(aaa==true)?0:(aab==true)?1: false ;


when box 1 is checked, it sets k= false, when box2 is checked it sets k= 'b2' and when both are checked it sets k = 'b2'... why is code doing so?

Peeyush
05-17-2012, 12:41 PM
Also having same problem with if...else code.



if (aaa=true&&aab==true) {k='b1'}
else if(aaa==true){k=0}
else if (aab==true) {k=1}

or


if(aaa==true){k=0}
else if (aab==true) {k=1}
else if (aaa=true&&aab==true) {k='b1'}

these two are working same as above two. i dont understand why :( is it some problem with logical operator &&?

Rowsdower!
05-17-2012, 01:28 PM
Also having same problem with if...else code.



if (aaa=true&&aab==true) {k='b1'}
else if(aaa==true){k=0}
else if (aab==true) {k=1}

or


if(aaa==true){k=0}
else if (aab==true) {k=1}
else if (aaa=true&&aab==true) {k='b1'}

these two are working same as above two. i dont understand why :( is it some problem with logical operator &&?

Well these two can't be working the exact same as the first two since the "k" values are different. That said, I haven't bothered to look very closely at your ternary version but the if/else versions above have some flaws. The first is with the syntax and the second is with the logic...

Version 1 error (syntax):

if (aaa=true&&aab==true) {k='b1'} //Note that you are ASSIGNING a value to "aaa" in this "if" statement, which is not what you mean to do. Add another "=" in there and this should work.
else if(aaa==true){k=0}
else if (aab==true) {k=1}

So you are assigning a value in the "if" statement itself, which is screwing things up for you. The value of "aaa" can always be assigned so the first check in the first "if" statement will always be true. That means that any time "aab" is set to true you will get a "k" value of "b1" no matter what "aaa" was set to. Furthermore, since your first "if" statement assigned a value of true to "aaa" the second "if" statement will now also always be true. So this code version will always return either "b1" or 0 for the value of "k" (depending only on whether or not "aab" was true). If you fix the first "if" statement (just double up on the "=" sign between "aaa" and "true" in the "if" statement) then this one should run correctly.

Version 2 error (logic):

if(aaa==true){k=0}
else if (aab==true) {k=1}
else if (aaa=true&&aab==true) {k='b1'} //If either "aaa" or "aab" is set to true you will have already used that "if" or "else if" statement's code and you will not get to this final "else if" - which means it may as well not even exist. The only values you will ever get with this code are 0 or 1.

So if they're both true you will have already exhausted one option or the other before getting to the "what if they're both true" option. Because of that, the value of "k" will never ever end up as "b1" in this version.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum