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 13 of 13
  1. #1
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts

    convert int to bool

    How do I do this equivalent function in javascript?
    Code:
    int x = 0;
    int y = 1; 
    
    convertBool(x);
    convertBool(y);
    
    private void convertBool(obj)
    {
      bool rtnB = Convert.ToBoolean(obj);
      string TorF = (!obj) ? "true" : "false" ;
      Console.WriteLine(TorF);
    } 
    
    // code would output
    // false
    // true
    I am trying to pass a bit by my function call
    Code:
    function fxn(obj){
      document.getElementById('box').style.display = (!obj) 'block' : 'none' ;
    }
    
    code somewhere else...
    onmouseover="fxn(1);" onmouseout="fxn(0)"
    I know I could do it via if obj != 0 or if obj == 0 but I want to be able to convert the bool- I know how to do it other ways but if js does not support a "one liner" I will do it via int compare
    Last edited by alykins; 12-27-2011 at 07:35 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You don't have to convert it to a boolean as javascript already allows you to mix variables and types (aside from arrays).

    This will work
    Code:
    function fxn(obj){
      document.getElementById('box').style.display = (!obj) ? 'block' : 'none' ;
    }
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I tried that exact line of code earlier (sry in original post I was just retyping it and missed the "?") and it spazed out... for now I have changed it to (obj > 0) to just use the int- i don't get why original code didn't work... it just stuck my "block" there

    html
    Code:
    <div id="MasterWrapper">  
      <div id="linx" onmouseover="popup(1);" onmouseout="popup(0);">
        <div id="pop"></div>
      </div>
    </div>
    and some CSS
    Code:
    #MasterWrapper
    {
        margin:30px auto;
        border: 3px ridge white;
        width:800px;
        height:500px;
        overflow:auto;
    }
    #linx
    {
        width:4px;
        height:500px;
        float:left;
        background:green;
        position:relative;
        z-index:0;
    }
    #pop
    {
        position:absolute;
        z-index:1;
        width:100px;
        height:500px;
        background:orange;
        display:none;
    }
    and then the two pieces of code in question...
    *working*
    Code:
    function popup(obj) {
                document.getElementById('pop').style.display = (obj > 0) ? 'block' : 'none';
            }
    and the original which as you, Aerospace already said (I thought js handled implicit conv)
    Code:
    function popup(obj) {
                document.getElementById('pop').style.display = (!obj) ? 'block' : 'none';
            }
    does not work... it gets the orange box stuck in the corner and then it never goes away until ironically you mouseover (sending it a 1 so it should be true -> 'block') it goes away and then when mouseout it comes back again ... ???

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I think it is working but is just going to negate what you give it e.g. if you give it 1, it becomes 0, if you give it 0, it becomes 1.
    So for example if you called
    Code:
    popup(1); // #pop stays at display='none'
    if you called
    Code:
    popup(0); // #pop goes to display='block'
    If you want it the other way then use this
    Code:
    function popup(obj) {
                document.getElementById('pop').style.display = (obj) ? 'block' : 'none';
            }
    And keep your html the same. If you want to keep your function as is then you need to change your html to this.
    Code:
    onmouseover="popup(0);" onmouseout="popup(1);"
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    alykins (12-27-2011)

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    hmmm... idk i am really lost as to why it is/isn't working... for "the heck of it" I fiddled with the obj/!obj and 0/1 sending combo... the code works either way with onmouse(0) and mouseout(1) with either (obj) or (!obj) ... I am wondering if the 1 is throwing it as null- for some reason VS keeps telling me that it is not a valid place to break so... anyways not the biggest deal ever- for now i changed it to (obj) and mouseover(0) and mouseout(1) even though "both" work just to keep it logic wise correct... if anyone else reaqds and knows "what the issue is" let me know otherwise I am marking resolved

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    and then the two pieces of code in question...
    *working*
    Code:
    function popup(obj) {
                document.getElementById('pop').style.display = (obj > 0) ? 'block' : 'none';
            }
    and the original which as you, Aerospace already said (I thought js handled implicit conv)
    Code:
    function popup(obj) {
                document.getElementById('pop').style.display = (!obj) ? 'block' : 'none';
            }
    does not work...

    (obj > 0) != (!obj)

  • #7
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Quote Originally Posted by Logic Ali View Post
    (obj > 0) != (!obj)
    well aware... (obj > 0) is treating obj as an int which is passed via fxn call; (!obj) is on the assumption that js will implicitly convert the datatype (presumably either 0|1) to false|true respectively and throw a js debug error if value other than 0|1 is passed.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I've told you what the issue was. You originally had
    Code:
    onmouseover="popup(1);" onmouseout="popup(0);"
    With your original function it would go like this.
    onmouseover
    Code:
    function popup(1) {
                document.getElementById('pop').style.display = (!1) ? 'block' : 'none'; // so now !1 becomes 0 hence why you don't get anything to show up.
            }
    onmouseout
    Code:
    function popup(1) {
                document.getElementById('pop').style.display = (!0) ? 'block' : 'none'; // so now !0 becomes 1 hence why it does work.
            }
    So with what I told you changing your function to get rid of the ! and then using your current html, would work or keeping your function but pass in a 0 for onmouseover and a 1 for onmouseout it would work. Honestly though you don't need javascript for any of this. You can use CSS
    Code:
    #linx:hover #pop {
    display:block;
    }
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Not to ask a really really dumb question, but...

    WHY are you passing something OTHER than boolean, in the first place???

    Code:
    ... onmouseover="popup(true);" onmouseout="popup(false);" ...
    and then
    Code:
    function popup(yesno) 
    {
                document.getElementById('pop').style.display = yesno ? 'block' : 'none'; 
    }
    Why would you *want* to use 1 and 0 or any other values, in place of true/false???

    Mystifies me utterly.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    @OldPedant because I am used to C# and in C# you can do that no prob bc there is a convert method- and there are times to use Convert.ToBoolean(int) hence why it is there-

    before there are ten posts about how this is not C# it is js I know that but I made my code based on that logic hence
    How do I do this equivalent function in javascript?
    the short and simple answer is there is no equiv js "one line" conversion of int to bool but what I thought Aerospace was saying was that it did it automatically... his statement of obj would still be 1 "...(!1)..." means that it does not automatically convert- i misread his original answer

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #11
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Well even !true = false and !false = true. Do you see the negation going on there? 1 and 0 can be booleans but yes just use true or false or as I stated don't even use javascript for this.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Quote Originally Posted by alykins View Post
    the short and simple answer is there is no equiv js "one line" conversion of int to bool
    Not true.

    Code:
    function fxn(int){
      document.getElementById('box').style.display = (int != 0) ? 'block' : 'none' ;
    }
    
    code somewhere else...
    onmouseover="fxn(1);" onmouseout="fxn(0)"
    The C# code you demonstrated (specifically, the Convert.ToBoolean(obj) call, though the function you wrote wouldn't compile because the obj argument had no type declaration) was attempting to convert obj to boolean, where obj was assumed to be of unknown type (and it would only work because of all the overloadings of ToBoolean, of course).

    That's a lot different than int to boolean. Which I emphasized by naming the variable int in the above code (since "int" has no special meaning in JavaScript code).

    However, there is *ALSO* a generic conversion of OBJECT REFERENCE to boolean in JavaScript. Notice that I am careful to say object REFERENCE. Just as in C# (and Java, but unlike C and C++), a variable in JavaScript can never contain an object, per se. A variable can only be a reference to an object.

    And the implicit conversion of a reference, of any kind, to boolean in JavaScript and C# and Java is the same: If the reference is null the boolean value is false. All non-null references are implicitly true. [If you see an exception in C# or Java code, it will be because the class of the given object has declared its own to-boolean method, assuming that's legal in C#, which I think it is.]

    So:
    Code:
    function fxn(objectReference){
      document.getElementById('box').style.display = (objectReference != null) ? 'block' : 'none' ;
    }
    
    code somewhere else...
    ... onmouseover="fxn(new Date());" onmouseout="fxn(null)" ...
    Now, it is also true that JavaScript will *implicitly* test an objectReference for null if you use it in a situation where a boolean is called for, so the above can be coded as simply
    Code:
    function fxn(objectReference){
      document.getElementById('box').style.display = objectReference ? 'block' : 'none' ;
    }
    
    code somewhere else...
    ... onmouseover="fxn(new Date());" onmouseout="fxn(null)" ...
    Personally, I find that kind of coding distasteful, as it hides the real meaning of the code, so I would alwyas use (objectReference != null) but you certainly don't have to do that.

    If you really wanted to do it, you could build a generic anything-to-boolean conversion function.
    Code:
    <script type="text/javascript">
    function toBoolean(x)
    {
        switch ( typeof x )
        {
            case "number": return x != 0;
            case "string": return true; 
            case "object": return x != null;
            case "undefined": return false;
            case "boolean" : return x;
            case "function" : return true;
        }
    }
    
    function testit( )
    {
        var values = [ 0, 1, 9999, 3.1415, null, new Date(), "bananas", "", [1,2,3], true, false, toBoolean ];
    
        for ( var v = 0; v < values.length; ++v )
        {
            value = values[v];
            document.write( 'toBoolean(' + value + ') is <b style="color: red;">' + toBoolean(value) + '</b><br/>' );
        }
    }
    
    testit();
    </script>
    (If you wished, you could define a blank string "" to be false. Since typeof "function" is really just another kind of object reference, and since a function reference is never null, returning true for that type is sensible. As is returning false for an undefined value.)

    But you certainly don't need that for your actual integer-to-boolean conversion.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    situation is resolved- it doesn't really matter anymore; only posting to addres my typo and your comment- no what i posted would not compile; i missed an important peice as i was typing- my function call in C# would be
    Code:
    private void convertBool(int obj)
    I forgot the type being passed- I used obj not in reference to object but just as a variable (yes I know to reserve it for good practices for objects- this was just an example) anyways yes good point what I had would not have compiled I was missing the data type- even if i was using type object I would have had to have (object obj) no matter what it would have error-ed out

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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