The Essential Guide to HTML5

(Greg DeLong) #1

CHAPTER 9


Code Explanation


var j; Iterates


var uniqueid; Used to create unique identifiers for elements


var thingelem; Holds the element


var out; Displays a message


for (i=0;i<secret.length;i++) { Iterates over the letters in the secret word


if (picked==secret[i]) { Says, “If the player guessed letter is equal to
this letter in secret...”


id = "s"+String(i); Constructs the identifier for this letter


document.getElementById(id).
textContent = picked;


Changes the text content to be the letter

not = false; Sets not to false


lettersguessed++; Increment the number of letters identified
correctly


if (lettersguessed==secret.length) { Says, “If the whole secret word has been
guessed...”


ctx.fillStyle=gallowscolor; Sets the color, which uses the brown of the
gallows but could be anything


out = "You won!"; Sets the message


ctx.fillText(out,200,80); Displays the message


ctx.fillText("Re-load the page to
try again.",200,120);


Displays another message

for (j=0;j<alphabet.length;j++) { Iterates over the whole alphabet


uniqueid = "a"+String(j); Constructs the identifier


thingelem = document.getElementById
(uniqueid);


Gets the element
Free download pdf