1. Home
  2. Computing & Technology
  3. JavaScript

Hangman Script Tutorial

page six

clr gif

 

<< Previous Page

function availLetter() {
document.write('<div class="hangt">');
for (var i = 0; i < al.length; i++) {
var ltr = String.fromCharCode(i+65);
if (al.charAt(i) == ltr)
document.write('&nbsp;&nbsp;');
else document.write('<a href="#" onclick="selectLetter(\''+i+'\')">'+ltr+'<\/a>&nbsp;');
if (i == 12) document.write('<br \/>');
}
document.write('<\/div>');
}

The availLetter function displays the letters that have not yet been chosen as separate links to allow the next selection to be made. The code loops through each of the characters in the selected guess string. The charAt method extracts the character at a specified position in the string which is compared to the letter of the alphabet corresponding to that location as extracted using fromCharCode. If they match the that letter has already been guessed and blanks are displayed. If not then the appropriate letter is displayed as a link that will call the selectLetter function passing the corresponding number. A break tag half way through splits the links onto two lines.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.