Guess That Letter


Writing a guess-that-letter is almost exactly like writing Guess That Number, except instead of storing a numerical value, we store a character from A – Z. When the user guesses, we use the ascii value of the character to hint to the user, higher or lower.

?View Code JAVASCRIPT
var guess_ascii, guess_char, guess_tries = 0;
 
function RandomRange(low,high)
{
    return( Math.floor((high-low-1)*Math.random()) + low );
}
 
/* http://sharkysoft.com/tutorials/jsa/content/018.html */
function AsciiValue(c)
{
	// restrict input to a single character
	c = c.charAt (0);
 
	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;
 
		// insert a % character into the string
		h = "%" + h;
 
		// determine the character represented by the escape code
		h = unescape (h);
 
		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}
 
/**
* @desc Think of a number from 1 to x
* @param number range to guess within
**/
function GuessChar( num )
{
	guess_tries = 0;
	guess_range = num;
	guess_ascii = RandomRange(97,122);				// a is ascii char 97, z is ascii char 122
	guess_char = String.fromCharCode( guess_ascii );
}
 
function Guess( frm )
{
	var g = frm.guess1.value.toLowerCase();				// we are dealing with a-z not A-Z
	var ascii = AsciiValue(g);
 
	if ( ascii < 97 || ascii > 122 )
	{
		alert("Please insert a letter from A - Z.");
		return;
	}
 
	guess_tries += 1;
	status = "Tries: "+ guess_tries;
 
	if ( g == guess_char )
	{
		if ( guess_tries == 1 )
			alert("Correct!  You guessed it in 1 try!  Wow, good job!!");
		else
			alert("Correct!  You guessed it in "+ guess_tries +" tries!");
 
		GuessOver();
	} else if ( guess_tries >= 10 ) {
 
		alert("Sorry, time's up. The letter was: "+ guess_char);
		GuessOver();
	} else if ( ascii < guess_ascii )
		frm.hint.value = "No, guess higher.";
	else if ( ascii > guess_ascii )
 
		frm.hint.value = "No, guess lower.";
}
 
/**
* @desc Prompt to start the guessing game over
**/
function GuessOver()
{
	if ( !confirm('Would you like to play again?') )
	{
		alert("Thanks for playing!  :)");
		location.reload();
	} else {
		GuessChar();
		document.forms.form1.hint.value = "Enter your Guess.";
	}
}
 
// initialize the guessing game
GuessChar();

View: Open Source Copy


Guess That Letter

I'm thinking of a letter between A and Z. Try to guess it in less than 10 tries.



Guess:


VN:F [1.9.22_1171]
Rating: 6.8/10 (6 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Guess That Letter, 6.8 out of 10 based on 6 ratings
Share/Save

,

  1. #1 by henChequeNesS on May 23, 2010 - 7:43 am

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian, iwspo.net

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. #2 by Wordpress Themes on June 12, 2010 - 6:08 am

    Nice fill someone in on and this enter helped me alot in my college assignement. Gratefulness you for your information.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
(will not be published)