www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Mozilla Firefox math problem.

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    resolved [RESOLVED] Mozilla Firefox math problem.

    The following code is used here to randomly pick a number in a bingo game. It works fine in IE but in Firefox it displays "undefined" for the number just drawn. When I placed an alert after the line:
    var r = Math.floor(Math.random() * j); // Pick one from those remaining
    to display r, I got "NaN". The genform() function worked fine though.

    TIA
    Code:
    <script language="JavaScript">
      var deck = new Array(75);
      var history = "";
      var j;
      
    function genform() {
      for (var i=0; i < 75;i++) {
        document.bingo.elements[i].value = formatR(i + 1);
        document.bingo.elements[i].style.color = "#cacaca";
        document.bingo.elements[i].style.backgroundColor = "#ffffff";
        deck[i] = i + 1;
      }
      document.bingo.lastcalled.value = "";
      document.bingo.history.value = "";
      history = "";
      j = 74;
    }
    
    function callone() {
       var bingolit = [ "B", "I", "N", "G", "O"] ;
       if (j < 0) {
         alert("No more numbers to draw");
         return 1;
       }
       var r = Math.floor(Math.random() * j); // Pick one from those remaining
       var drawn = deck[r];
       deck[r] = deck[j]; // Move last of those remaining to slot just chosen
       if (j <74) {
         if (history.length > 0) history+= "\n";
    //	 history += document.bingo.lastcalled.value;
    	 history = document.bingo.lastcalled.value + "\n" + history;
    	 document.bingo.history.value = history;
       }	   
       document.bingo.lastcalled.value = bingolit[Math.floor((drawn-1)/15)] + ' ' + drawn;
       document.bingo.elements[drawn - 1].style.color = "#000000";
       document.bingo.elements[drawn - 1].style.backgroundColor = "#ffff00";
       j--;
    }
    
    function formatR (x) { if (x < 10) return '  ' + x; else return x;} //Right justify

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    I just found that j is not treated as a global variable. Both functions modify it (number of unchosen balls remaining).

    genform() is run on load and when the user clicks the "Reset Board" button.
    Last edited by wbport; 08-25-2010 at 09:41 AM.

  3. #3
    Join Date
    Aug 2010
    Posts
    7
    Im not sure where the j would be incrementing with the data that i see. Mozilla i beleive is posting J from the top global value of (var j) and not from the posted j=74 on the first script. How is the Second script initiated?

    possibly run from original and carry the j over as (j)....

    im not so sure here

  4. #4
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Perhaps Mozilla is not using the global variable 'j' because it is not initialized. It is declared but not defined. Try var j = 0;
    Computer programmer / web developer.

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    I have a new link to the testing version of the page: bingo_test.htm. I gave j a value of zero but I don't get the "in genform j =" alert, but it runs the "for" loop just fine.

    The second script "callone()" is started when the "Draw Number" button is clicked on. J is set to 74 (shows count of numbers remaining [+ 0th] to select from) in the genform() script and is decremented near the end of the callone() script.

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    history is a reserved word, change the name of this variable.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    resolved

    Quote Originally Posted by Fang View Post
    history is a reserved word, change the name of this variable.
    Many thanks! That was it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles