SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random number generation different for the IEs?

    Hi javascript masters,

    First, here's my page with the problem. All the numbers should match. It's not working right in any version of IE I test it in (6, 7 and 8).

    Sorry in advance if my explanation is a little confusing. I'm also using jQuery, fyi.

    When you click the button to stop the numbers, I'm setting a variable named winner to a number (3, 4 or 5). Right now, I have it set to 5. This number determines how many of the slots match:

    Code JavaScript:
    var winner = 5;

    I'm then generating a random number (0-8) to base all the other numbers off of . Three numbers will always match, so I'm setting two more variables to match that random number.

    Code JavaScript:
    var randomnum = Math.floor(9*Math.random());
    var randomnum2 = randomnum;
    var randomnum3 = randomnum;

    Then I do some conditionals to check the value of winner. If it's 4, I set the 4th number to random number, then generate a random number for the 5th spot while it doesn't equal randomnum. I do something similar if winner is set to 5.

    Code JavaScript:
    if(winner == 4)
    {
        var randomnum4 = randomnum;
        var randomnum5 = Math.floor(9*Math.random());
        while(randomnum5 == randomnum){
               randomnum5 = Math.floor(9*Math.random());
        }
    }else{
           var randomnum4 = Math.floor(9*Math.random());
           while(randomnum4 == randomnum){
              randomnum4 = Math.floor(9*Math.random());
           }
           var randomnum5 = Math.floor(9*Math.random());
           while(randomnum5 == randomnum){
               randomnum5 = Math.floor(9*Math.random());
           }
    }//End win = 4
    // for 5 matching numbers
    if(winner == 5)
    {
        var randomnum4 = randomnum;
        var randomnum5 = randomnum;
    }else{
         if(winner == 4){
         var randomnum4 = randomnum;
         }else{
    	  var randomnum4 = Math.floor(9*Math.random());
        	  while(randomnum4 == randomnum){
            	randomnum4 = Math.floor(9*Math.random());
              }
          }
          var randomnum5 = Math.floor(9*Math.random());
          while(randomnum5 == randomnum){
              randomnum5 = Math.floor(9*Math.random());
          }
    }

    Then I populate an array with the 5 numbers, shuffle it and assign each number to a new variable.

    Code JavaScript:
    var slotArray = [randomnum, randomnum2, randomnum3, randomnum4, randomnum5];
    slotArray.sort(function(){ return 0.5 - Math.random()});
    var slotnum1 = slotArray[0];
    var slotnum2 = slotArray[1];
    var slotnum3 = slotArray[2];
    var slotnum4 = slotArray[3];
    var slotnum5 = slotArray[4];

    Finally, I use a setTimeout method to stop each slot.

    Code JavaScript:
    setTimeout(function(){
    $("#slot1").trigger( 'stop' ).trigger( 'goto', slotnum1 );
    }, 5);
    setTimeout(function(){
    $("#slot2").trigger( 'stop' ).trigger( 'goto', slotnum2 );
    }, 505);
    setTimeout(function(){
    $("#slot3").trigger( 'stop' ).trigger( 'goto', slotnum3 );
    }, 1005);
    setTimeout(function(){
    $("#slot4").trigger( 'stop' ).trigger( 'goto', slotnum4 );
    }, 1505);
    setTimeout(function(){
    $("#slot5").trigger( 'stop' ).trigger( 'goto', slotnum5 );
    }, 2005);

    trigger is a method of the SerialScroll jQuery plugin. It indexes each element under #slot(x).

    So:

    Code HTML4Strict:
    <div id="slot1">
       <div>1</div><!-- index 0 -->
       <div>2</div><!-- index 1 -->
       <div>3</div><!-- index 2 -->
       <div>4</div><!-- index 3 -->
       <div>5</div><!-- index 4 -->
       <!-- and so on.. -->
    </div>

    The goto trigger sends the scroll to the specified index according to slotnum(1-5).

    I'm waiting to hear back from the plugin developer to see if it's something to do with that.

    However, I just want to make sure my logic in generating those numbers is sound. It works perfectly in FF, Opera and Chrome but (surprise!) not in any of the IEs (tested in 6, 7 and 8).

    So, any help from a javascript wizard would be much appreciated.

    Thanks!
    No, I REALLY dislike having to use Joomla.

  2. #2
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to log javascript output in IE7 like you can with console.log to FF's firebug?
    No, I REALLY dislike having to use Joomla.


Bookmarks

Posting Permissions

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