Double Trouble?



  • I noticed this javascript at http://www.unityhealth.com and I was confused, perhaps someone can enlighten me:

    function reDoIE(){
       if(is.ns6) {
          available_width = innerWidth;
          available_height = innerHeight;
       } else if(is.ie4 || is.ie5 || is.ie55 || is.ie6) {
          available_width=document.body.clientWidth;
          available_height=document.body.clientHeight;
       }
       var imBG = document.getElementById("bgImage");
       imBG.width = (available_width * 1.00);
       imBG.height = (available_height * 1.00);
    }

    Last I checked multiplying a number by 1 returns the same number!  The only "benefit" I can think of is maybe they needed to cast to a double for some strange absurd reason



  • More code from the above site:

    function choseBg() {
       var backgr1="/intranet/groups/pubweb/documents/unity_image/unity_bkimage01.jpg";
       var backgr2="/intranet/groups/pubweb/documents/unity_image/unity_bkimage02.jpg";
       var backgr3="/intranet/groups/pubweb/documents/unity_image/unity_bkimage03.jpg";
       var backgr="/intranet/groups/pubweb/documents/unity_image/unity_bkimage04.jpg";
       var cur=Math.round(8*Math.random());
       backgr=backgr1;
       if (cur<=2) backgr=backgr1;
       else if (cur<=4) backgr=backgr2;
       else if (cur<=6) backgr=backgr3;
       return backgr;
    }

    I'll let you figure out the "oddness" going on there!



  • I don't see the problem with the first one. What if the multiplicative property of multiplying by 1 should ever change? Maybe this is quantum mathematics in use, and we're just not smart enough to realize it.

    As for the second post, I hope whoever created image #4 isn't offended that their image will never be seen. And I know it might be a nit-picky thing, but "choseBg"? Since "chose" is past tense, I'd imagine this function does something after a new background was chosen. If it's supposed to do the choosing, then "chooseBG" might be better.

    Once again, Javascript solves problems like world hunger.



  • Here's my list:

    1. background 4 is never usered
    2. the specify the full page for each image instead of something like "path"+cur+".jpg"
    3. they choose a random number from 1-8 and then divide by 2...
    4. he defaults the background to 4 and then overwrites it two lines later with background 1
    5. the naming of the method indicated the background has already been chosen
    6. The path indicates an intranet image... is the intranet open to the public?

    6 that's all I got... 6 wtf's for 11 lines of code... not a bad ratio!



  • DrJames: The concept he's using with the random numbers is familiar to me. Check it out:

    The random number he generates will be between 0 and 7 inclusive, because I don't think you'll ever get 8 with that calculation. If you have four choices you want randomly chosen, then you'd break it up into sections that are two numbers wide. If it's 0 or 1, choose one background. If it's 2 or 3, choose this one, and so on. All the I think he was going for something like this:

    if (cur < 2)

        backgr = backgr1;

    elseif(cur < 4)

        backgr = backgr2;

    elseif(cur < 6)

        backgr = backgr3;

    else

        backgr = backgr4;

     

    But no matter how you look at it, he cocked it up. It's still just as many WTFs as you counted.



  • @Manni said:

    DrJames: The concept he's using with the random numbers is familiar to me. Check it out:

    The random number he generates will be between 0 and 7 inclusive, because I don't think you'll ever get 8 with that calculation. If you have four choices you want randomly chosen, then you'd break it up into sections that are two numbers wide. If it's 0 or 1, choose one background. If it's 2 or 3, choose this one, and so on. All the I think he was going for something like this:

    if (cur < 2)

        backgr = backgr1;

    elseif(cur < 4)

        backgr = backgr2;

    elseif(cur < 6)

        backgr = backgr3;

    else

        backgr = backgr4;

     

    But no matter how you look at it, he cocked it up. It's still just as many WTFs as you counted.

    Correct me if I'm wrong but since Math.Round is not specified as ceiling or floor,  I'm assuming its rounding to the "closest integer" which would mean that I was wrong in saying from 1-8, it's actually from 0-8.

    I still don't get why you would break it into two number sections... just do Math.Floor(4*random) which will give you 0-3, then do

    switch (randomnumber)

    case 0: do this; break;
    case 1: do that; break;

    etc.



  • @DrJames said:

    @Manni said:

    DrJames: The concept he's using with the random numbers is familiar to me. Check it out:

    The random number he generates will be between 0 and 7 inclusive, because I don't think you'll ever get 8 with that calculation. If you have four choices you want randomly chosen, then you'd break it up into sections that are two numbers wide. If it's 0 or 1, choose one background. If it's 2 or 3, choose this one, and so on. All the I think he was going for something like this:

    if (cur < 2)

        backgr = backgr1;

    elseif(cur < 4)

        backgr = backgr2;

    elseif(cur < 6)

        backgr = backgr3;

    else

        backgr = backgr4;

     

    But no matter how you look at it, he cocked it up. It's still just as many WTFs as you counted.

    Correct me if I'm wrong but since Math.Round is not specified as ceiling or floor,  I'm assuming its rounding to the "closest integer" which would mean that I was wrong in saying from 1-8, it's actually from 0-8.

    I still don't get why you would break it into two number sections... just do Math.Floor(4*random) which will give you 0-3, then do

    switch (randomnumber)

    case 0: do this; break;
    case 1: do that; break;

    etc.



    Real men would put functions in an array, and do something like:
    [code]actionsArray[Math.Floor(4*random)]()[/code]
    No more comparing! Ever!

    I also love this bit:

      if(is.ns6) {
          available_width = innerWidth;
          available_height = innerHeight;
       } else if(is.ie4 || is.ie5 || is.ie55 || is.ie6) {
          available_width=document.body.clientWidth;
          available_height=document.body.clientHeight;
       }


    In other words,

    else {
      //throw an error because of the undefined variables
    }


  • @DrJames said:

    Correct me if I'm wrong but since Math.Round is not specified as ceiling or floor,  I'm assuming its rounding to the "closest integer" which would mean that I was wrong in saying from 1-8, it's actually from 0-8.


    actually IIRC it performs a banker's round, which means .5 is rounded down instead of up



  • t-bone wrote:

    a banker's round, which means .5 is rounded down instead of up



    Actually, that's a Common Round. Banker's Rounding rounds less than 0.5 down, more than 0.5 (including 0.51 etc) up, and exactly 5 so that the digit to the left of it winds up even.



  • excuse me while I correct myself (maybe it'd help if I read what I was replying to)...



    I think that rounding down 0.5s would be an uncommon round. I've personally never encountered it.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.