Note that there are some explanatory texts on larger screens.

plurals
  1. POlearning javascript, need clarification on variables values
    text
    copied!<p>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)</p> <pre><code>window.onload = newCard; function newCard() { for(var i=0; i&lt;24; i++){ setSquare( i ) } function setSquare (thisSquare) { var currSquare = "square" + thisSquare; var newNum = Math.floor(Math.random()*75 )+1 document.getElementById(currSquare).innerHTML = newNum; } } </code></pre> <ol> <li><p>the first function <code>newCard</code>, after the loop is done, sets its final values and then passes it <code>setSquare</code> which is the next function below it. What I dont get is...assuming that the parsing goes from top to bottom, <code>setSquare</code> isnt defined yet so shouldnt it return an error?</p></li> <li><p>My confusion about parsing and distributing values aside, on the second function.</p> <pre><code>var currSquare = "square" + thisSquare; </code></pre> <p><code>setSquare</code> in the second function now has the number values from function one that it was passed. variable <code>currSquare</code> is taking the <code>"square"</code> id from main HTML table and adding into it the value from function 1 above. my quesiton is, how does javascript know that <code>"square"</code> in function 2 is a id and not a string? further more because of it,</p> <pre><code>document.getElementById(currSquare).innerHTML = newNum; </code></pre> <p>there IS no ID on main page of <code>currSquare</code> so how can it "get" it and pass it <code>newNum</code> if there is no such thing? <code>currSquare</code> is a variable.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload