I'm doing a JS tutorial and long story short, we have to get certain amounts of numbers and pass them to HTML table/cells with ids of "square". the code works but im not sure how. Im confused in 2 parts. (below the code)
window.onload = newCard;
function newCard() {
for(var i=0; i<24; i++){
setSquare( i )
}
function setSquare (thisSquare) {
var currSquare = "square" + thisSquare;
var newNum = Math.floor(Math.random()*75 )+1
document.getElementById(currSquare).innerHTML = newNum;
}
}
the first function
newCard, after the loop is done, sets its final values and then passes itsetSquarewhich is the next function below it. What I dont get is...assuming that the parsing goes from top to bottom,setSquareisnt defined yet so shouldnt it return an error?My confusion about parsing and distributing values aside, on the second function.
var currSquare = "square" + thisSquare;setSquarein the second function now has the number values from function one that it was passed. variablecurrSquareis taking the"square"id from main HTML table and adding into it the value from function 1 above. my quesiton is, how does javascript know that"square"in function 2 is a id and not a string? further more because of it,document.getElementById(currSquare).innerHTML = newNum;there IS no ID on main page of
currSquareso how can it "get" it and pass itnewNumif there is no such thing?currSquareis a variable.