1. Technology

Your suggestion is on its way!

An email with a link to:

http://javascript.about.com/library/blhangt4.htm

was emailed to:

Thanks for sharing About.com with others!

Hangman Script Tutorial

page four

clr gif

 

<< Previous Page

var answer = trans2(opts[opt]);
function trans2(op) {
var opn = '';
for (var i = 0; i < op.length; i++) {
var ch = op.substr(i,1);
if (ch == dc.substr(27,1)) break;
if (ch == dc.substr(0,1)) opn += ' ';
else opn += String.fromCharCode(dc.indexOf(ch)+64);
}
return opn;
}

This code decrypts the selected answer from within the answer array. The trans2 function translates each character in turn using the substr method to extract the individual characters. The last character in the dc string indicates the end of the answer while the first character in the dc string indicates a blank. The entries in between represent the letters of the alphabet and these are translated by using the indexOf method to determine which position in the dc string matches the current character. Since the Alphabet starts with character 65 in ASCII, adding 64 to the position within the string gives us the ASCII code fir the letter and using the fromCharCode global string method translated that into the letter itself.

See More About

©2014 About.com. All rights reserved.