Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need help understanding a couple of things in javascript

1. ## 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

• Originally Posted by javanewbie7
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

•

#### Posting Permissions

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