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

    javascript code producing unexpected results, a ques related to Conditional Operator.

    this is my javascript code:
    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:

    Code:
    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?
    Last edited by Peeyush; 05-17-2012 at 03:06 PM.
    Everything is simpler with jQuery!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Also having same problem with if...else code.

    Code:
    	if (aaa=true&&aab==true) {k='b1'}
    	else if(aaa==true){k=0}
    	else if (aab==true) {k=1}
    or
    Code:
    	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 &&?
    Everything is simpler with jQuery!

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by Peeyush View Post
    Also having same problem with if...else code.

    Code:
    	if (aaa=true&&aab==true) {k='b1'}
    	else if(aaa==true){k=0}
    	else if (aab==true) {k=1}
    or
    Code:
    	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):
    Code:
    	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):
    Code:
    	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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Peeyush (05-17-2012)


  •  

    Posting Permissions

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