Another Odd Question...

Discussion in 'JavaScript' started by extrickster, Jan 27, 2011.

  1. #1
    Hello Digital Point,

    I was reading my JavaScript book the other day... it shows an 'Bingo Card' question/example but the book does not really explain the logic behind question/example. I typed it in several times today trying to grasp the logic... but I do not get the logic so I need an explanation of how it works...

    window.onload = newCard;
    
    function newCard() {
    	if (document.getElementById) {
    		for (var i=0; i<24; i++) {
    			setSquare(i);
    		}
    	}
    	else {
    		alert("Sorry, your browser doesn't support this script");
    	}
    }
    
    function setSquare(thisSquare) {
    	var currSquare = "square" + thisSquare;
    	var colPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);
    	var newNum = (colPlace[thisSquare] * 15) + Math.floor(Math.random() * 15) + 1;
    
    	document.getElementById(currSquare).innerHTML = newNum;
    }
    Code (markup):
    I understand the functions, methods, and parameters work... but this is the line of code that causing my confusion.
    var newNum = (colPlace[thisSquare] * 15) + Math.floor(Math.random() * 15) + 1;
    Code (markup):
     
    Last edited: Jan 27, 2011
    extrickster, Jan 27, 2011 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    The script is filling in the bingo card by rows and columns. The line of code you ask about generates a random number based on 15 times the column plus a random number between 1 and 15. The purpose is to create a bingo card with numbers that fall within a specific range in each column.
     
    rainborick, Jan 27, 2011 IP
  3. extrickster

    extrickster Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So does it work like?...

     
    extrickster, Jan 28, 2011 IP