Hangman Script Tutorialpage six |
|
More of this FeaturePage One Page Two Page Three Page Four Page Five Page Seven Page Eight
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(' '); else document.write('<a href="#" onclick="selectLetter(\''+i+'\')">'+ltr+'<\/a> '); 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. |

