HTML and CSS Reference
In-Depth Information
Code
Explanation
slots[i] = -100;
Used values will be 0 to 19
}
Close of
for
loop
for(i=0;i<nq;i++) {
Start of
for
loop to choose
nq
country/capital pairs
Recall our
nq
is set to 4, 4 country-capital pairs
do {c = Math.floor(Math.
random()*facts.length);}
Start of do/while loop. What is in the brackets is done at
least once. The variable
c
is set to a random value 0 to 1
less than the length of the array.
while (facts[c][2]==true)
Do this again if this inner array (country/capital pair) has
been chosen
facts[c][2]=true;
Outside of loop, now set this country/capital pair array as
being used
uniqueid = "c"+String(c);
Construct the id for the country block
d =
document.createElement
('country');
Create an
html
element of type
country
d.innerHTML = (
Set its
innerHTML
to be
"<div class='thing'
id='"+uniqueid+"'>placeholder</div>");
… a
div
of class
thing
with the
id
. The contents of the
element will be changed
document.body.appendChild(d);
Add this element to the document as a child of the
body
element
thingelem =
document.getElementById(uniqueid);
Get a pointer to the element just created
thingelem.textContent=
facts[c][0];
Set its
textContent
to the country name
thingelem.style.top =
String(my)+"px";
Position it vertically by changing the
top
style
thingelem.style.left =
String(mx)+"px";
… and horizontally by changing the
left
style