Hangman Script Tutorialpage seven |
|
More of this FeaturePage One Page Two Page Three Page Four Page Five Page Six Page Eight
function displayAnswer() {
var correct = ''; document.write('<div class="hanga"> <br \/>'); for (var i = 0; i < answer.length; i++) { if (answer.substr(i,1) == ' ') { document.write(' '); correct += ' '; } else { var ltr = ''; if (win == 0) ltr = al.charAt(answer.charCodeAt(i)-65); else ltr = answer.substr(i,1); document.write(ltr + ' '); correct += ltr; } } document.write('<\/div>'); if (win == 0) { var opty = Math.floor(Math.random()*165)*71 + opt; if (wr.charAt(wx) == 'z') top.location = page + '?win=1&opt='+opty+'&w='+wr.charAt(wx); if (correct == answer.toUpperCase()) top.location = page + '?win=4&opt='+opty+'&w='+wr.charAt(wx); } } The displayAnswer function displays the answer on the screen. Each of the letters in the answer (apart from blanks) displays the character from the appropriate position in the selected guess string. This means that the appropriate letter will be displayed if it has already been guessed and a dash will be displayed if the letter has not yet been guessed (blanks always display as blanks). At the same time as displaying the entries on the screen they are also stored into the correct string. If the game isn't yet over (win==0) then the correct answer is once again encrypted and a test is then made to see if the maximum number of wrong guesses have been made (wr.charAt == 'z') in which case the page will be reloaded passing win=1 to indicate that the game is lost. If the value in the correct variable matches exactly to the correct answer then the page will be reloaded passing win=4 to indicate that the game has been won. |

