# [web] JavaScript Guessing Game Snag

This topic is 3798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, all! I'm a beginning javascript coder and I'm trying to do a rather bare-bones Alphabet Guessing game as a learning exercise. The idea is that the computer picks a letter from A to Z, the player enters a guess, and the computer gives feedback to the user about the guess. So, what's the problem? Well, currently, it's that the guess seems to register as "A," no matter what is actually entered. Can someone help me figure out where I'm going wrong? Code is below... (general comments about imporving my coding technique are welcome also; there may are probably better ways to do some things) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Alphabet Game</title> <script language="javascript" type="text/javascript"> function startGame() { // Reset hint document.forms[0].Hint.value = "Waiting for guess..."; // Set counter to 0 document.forms[0].counter.value = 0; // Give instructions document.forms[0].Message.value = "I will pick a letter between A and Z." + " Where\nprompted, type in your guess, and then click \n'Submit Guess.'" + " I will tell you whether your \nguess is too close to the beginning of the \nalphabet, too close to the end of the \nalphabet, or correct." + " Watch the Guess Counter \nto see how many tries it takes for you to \nguess my letter." + " You can submit a guess \nwhenever you're ready."; // Choose letter (a long switch statement) Num = Math.round(Math.random() * 26); switch(Num) { case 1: Letter = 'A'; break; case 2: Letter = 'B'; break; case 3: Letter = 'C'; break; case 4: Letter = 'D'; break; case 5: Letter = 'E'; break; case 6: Letter = 'F'; break; case 7: Letter = 'G'; break; case 8: Letter = 'H'; break; case 9: Letter = 'I'; break; case 10: Letter = 'J'; break; case 11: Letter = 'K'; break; case 12: Letter = 'L'; break; case 13: Letter = 'M'; break; case 14: Letter = 'N'; break; case 15: Letter = 'O'; break; case 16: Letter = 'P'; break; case 17: Letter = 'Q'; break; case 18: Letter = 'R'; break; case 19: Letter = 'S'; break; case 20: Letter = 'T'; break; case 21: Letter = 'U'; break; case 22: Letter = 'V'; break; case 23: Letter = 'W'; break; case 24: Letter = 'X'; break; case 25: Letter = 'Y'; break; case 26: Letter = 'Z'; break; } // Focus on guess document.forms[0].Guess.focus(); } function submitGuess() { // Update counter, increase its value by one document.forms[0].counter.value++; // Focus on guess document.forms[0].Guess.focus(); // Convert guess to numeric data (for capital letter guesses) if (document.forms[0].Guess.value = 'A') { Trial = 1; } else if (document.forms[0].Guess.value = 'B') { Trial = 2; } else if (document.forms[0].Guess.value = 'C') { Trial = 3; } else if (document.forms[0].Guess.value = 'D') { Trial = 4; } else if (document.forms[0].Guess.value = 'E') { Trial = 5; } else if (document.forms[0].Guess.value = 'F') { Trial = 6; } else if (document.forms[0].Guess.value = 'G') { Trial = 7; } else if (document.forms[0].Guess.value = 'H') { Trial = 8; } else if (document.forms[0].Guess.value = 'I') { Trial = 9; } else if (document.forms[0].Guess.value = 'J') { Trial = 10; } else if (document.forms[0].Guess.value = 'K') { Trial = 11; } else if (document.forms[0].Guess.value = 'L') { Trial = 12; } else if (document.forms[0].Guess.value = 'M') { Trial = 13; } else if (document.forms[0].Guess.value = 'N') { Trial = 14; } else if (document.forms[0].Guess.value = 'O') { Trial = 15; } else if (document.forms[0].Guess.value = 'P') { Trial = 16; } else if (document.forms[0].Guess.value = 'Q') { Trial = 17; } else if (document.forms[0].Guess.value = 'R') { Trial = 18; } else if (document.forms[0].Guess.value = 'S') { Trial = 19; } else if (document.forms[0].Guess.value = 'T') { Trial = 20; } else if (document.forms[0].Guess.value = 'U') { Trial = 21; } else if (document.forms[0].Guess.value = 'V') { Trial = 22; } else if (document.forms[0].Guess.value = 'W') { Trial = 23; } else if (document.forms[0].Guess.value = 'X') { Trial = 24; } else if (document.forms[0].Guess.value = 'Y') { Trial = 25; } else { Trial = 26; } // Convert guess to numeric data (for lowercase letter guesses) if (document.forms[0].Guess.value = 'a') { Trial = 1; } else if (document.forms[0].Guess.value = 'b') { Trial = 2; } else if (document.forms[0].Guess.value = 'c') { Trial = 3; } else if (document.forms[0].Guess.value = 'd') { Trial = 4; } else if (document.forms[0].Guess.value = 'e') { Trial = 5; } else if (document.forms[0].Guess.value = 'f') { Trial = 6; } else if (document.forms[0].Guess.value = 'g') { Trial = 7; } else if (document.forms[0].Guess.value = 'h') { Trial = 8; } else if (document.forms[0].Guess.value = 'i') { Trial = 9; } else if (document.forms[0].Guess.value = 'j') { Trial = 10; } else if (document.forms[0].Guess.value = 'k') { Trial = 11; } else if (document.forms[0].Guess.value = 'l') { Trial = 12; } else if (document.forms[0].Guess.value = 'm') { Trial = 13; } else if (document.forms[0].Guess.value = 'n') { Trial = 14; } else if (document.forms[0].Guess.value = 'o') { Trial = 15; } else if (document.forms[0].Guess.value = 'p') { Trial = 16; } else if (document.forms[0].Guess.value = 'q') { Trial = 17; } else if (document.forms[0].Guess.value = 'r') { Trial = 18; } else if (document.forms[0].Guess.value = 's') { Trial = 19; } else if (document.forms[0].Guess.value = 't') { Trial = 20; } else if (document.forms[0].Guess.value = 'u') { Trial = 21; } else if (document.forms[0].Guess.value = 'v') { Trial = 22; } else if (document.forms[0].Guess.value = 'w') { Trial = 23; } else if (document.forms[0].Guess.value = 'x') { Trial = 24; } else if (document.forms[0].Guess.value = 'y') { Trial = 25; } else { Trial = 26; } // Process guesses, see what to tell the player if(Trial > Num) { // If the player’s guess is past the letter, then do this Hint = "too close to the end of the alphabet"; outPut(); //executes this function } else if(Trial < Num) { // If the player’s guess is before the letter, then do this Hint = "too close to the beginning of the alphabet"; outPut(); } else if(Trial == Num) { // If the player’s guess is the correct letter, then do this Hint = "absolutely correct"; playAgain(); //executes this function } } function outPut() { // Output the hint document.forms[0].Hint.value = "Your guess of "+document.forms[0].Guess.value+" is "+Hint+". Guess again."; document.forms[0].Guess.value = ""; } function playAgain() { // This displays the victory text and prompts the user for another game document.forms[0].Hint.value = "You are so right!"; // This opens a dialoge window for the user to confirm var again = confirm("Congratulations! You guessed my letter, " +Letter+ ", in " +document.forms[0].counter.value + " guesses.\nWould you like to play again?") // If the user confirms then another game is started if(again){startGame()}; } </script> </head> <body>

# The Alphabet Game

Welcome to the Alphabet Game! The Alphabet Game is a letter guessing game. I will pick a letter, and you get to try to guess it. Don't worry... I'll give you hints along the way to help you out!

Start a new game<br /> <br />

<form id="Game" name="Game"> <table width="750" border="0"> <tr> <td>Messages and Instructions:</td> <td> </td> </tr> <tr> <td><textarea name="Message" rows="7" cols="50" onfocus="blur()"></textarea></td> <td> </td> </tr> <tr> <td>My hint:</td> <td> </td> </tr> <tr> <td><input type="text" name="Hint" size="60" /></td> <td> </td> </tr> <tr> <td>Your guess:</td> <td>Guess counter - How quickly can you guess the letter?</td> </tr> <tr> <td><input type="text" name="Guess" size="30" maxlength="4" /></td> <td><input type="text" size="2" name="counter" /></td> </tr> <tr> <td><br /> <input type="button" value="Submit Guess" onclick="submitGuess(),document.forms[0].Guess.focus()" /></td> <td> </td> </tr> </table> </form> </body> </html>

##### Share on other sites
I see a few things.

Definitely the BIG one is that in your "if" statements inside SubmitGuess you are using "=" instead of "==". So, you are basically assigning 'A' to your text input box.

Also, in general I noticed that you use variables inside functions and expect them to be global and accessible from other functions. For example, in the outPut function you use the Hint variable, which is set somewhere else, but you never give it an explicit scope. You should explicitly do something like this:

var Hint;

inside of the <script> block.

Also, your random number generation may not work all the time. Round may round down to 0, in which case you dont get A. You should do:

Num = int(Math.random*26); // will return 0 to 25

Other suggestions:

1. You can simplify your random selection code and detection code with something like this:

var aLetters = ['A','B','C','D', ... , 'Z'];
Num = int(Math.random() * 26);
Letter = aLetters[Num];

testing:

var sLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var inChar = document.forms[0].Guess.value.toUpperCase().charAt(0);
var Trial = sLetters.indexOf(inChar);

Hope this helps.

Brian

##### Share on other sites
Brian,

Thank you!!!

Thanks again...

(Now I'm off to try to fix my code...) :)

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002365
×