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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    128
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Need help understanding a couple of things in javascript

    Hey all,

    I'm trying to fully understand javascript and have a couple of questions I was hoping you could answer for me.

    I'm working through a book and in the book I'm creating a Bingo card that generates a random number. I'm pasting the full script below just in case it's needed.

    Code:
    function initAll () {
    	 for(var i=0; i<24; i++) {
    	setSquare(i);		 
       }
    }
    
    function setSquare(thisSquare) {
      var currSquare= "square" + thisSquare;
      var colPlace= new Array  (0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
      var colBasis=colPlace[thisSquare] *15;
      var newNum=colBasis +getNewNum()+1;
    	
    document.getElementById(currSquare).innerHTML=newNum;
    }
    
    function getNewNum() {
      return Math.floor(Math.random() *15);
    }
    //-->
    </script>

    1. What does the [thisSquare] mean in this line of code that is bolded? I mean, I know thisSquare is whatever i is, but am I multiplying it by colPlace or what?

    2. Lastly I'm confused by this part of the above code

    Code:
    var newNum=colBasis +getNewNum()+1;
    Why add the +1? I mean, i think getNewNum is set to pull a random number between 1 and 14. If you want to pull a number between 1 and 15, why not multiple Math.random() *16 instead of 15.

    Am i missing something?

    Sorry, I'm just not understanding why use the +1

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    Quote Originally Posted by javanewbie7 View Post
    Hey all,

    I'm trying to fully understand javascript and have a couple of questions I was hoping you could answer for me.

    I'm working through a book and in the book I'm creating a Bingo card that generates a random number. I'm pasting the full script below just in case it's needed.

    Code:
    function initAll () {
    	 for(var i=0; i<24; i++) {
    	setSquare(i);		 
       }
    }
    
    function setSquare(thisSquare) {
      var currSquare= "square" + thisSquare;
      var colPlace= new Array  (0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
      var colBasis=colPlace[thisSquare] *15;
      var newNum=colBasis +getNewNum()+1;
    	
    document.getElementById(currSquare).innerHTML=newNum;
    }
    
    function getNewNum() {
      return Math.floor(Math.random() *15);
    }
    //-->
    </script>

    1. What does the [thisSquare] mean in this line of code that is bolded? I mean, I know thisSquare is whatever i is, but am I multiplying it by colPlace or what?

    2. Lastly I'm confused by this part of the above code

    Code:
    var newNum=colBasis +getNewNum()+1;
    Why add the +1? I mean, i think getNewNum is set to pull a random number between 1 and 14. If you want to pull a number between 1 and 15, why not multiple Math.random() *16 instead of 15.

    Am i missing something?

    Sorry, I'm just not understanding why use the +1
    colPlace[thisSquare] -

    imagine the value of thisSquare is 3
    colPlace[3] means get the third value in the array colPlace, because its also got a value at 0 (colPlace[0]), its actually the 4th number in
    if colPlace is Array(0,1,2,3,16) then the value of colPlace[3] is 3, colPlace[2] is 2, colPlace[1] is 1, colPlace[0] is 0, colPlace[4] is 16


    Code:
    var newNum=colBasis +getNewNum()+1;
    getNewNum() returns a value between 0 and 14, we add one to make it a number between 1 and 15, only reason I can think of is if even if getNum() returns 0, the number is always changed
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/


  •  

    Posting Permissions

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