![]() |
Number Guessing game
Hello!
So, here is my problem. I have to write a number-guessing game that prompts the visitor to guess the right number in a maximum of 4 attempts. If the visitor guesses the target number within 4 tries, I want to write an alert that congratulates them as well as indicates the number of tries taken. If they guess incorrectly, I want to prompt with "Try again." And I finally want to keep track of each number they typed with a document.write. I have something like this, but its just not working out: Code:
<script type="text/javascript"> |
Code:
alert("Congratulations, you guessed it in " + i + " tries!Code:
else if (count=55){Code:
else if (count == 55){You should do the if's based on the prompt reply. Your hung up in the loop. |
Use the error console
|
Have a look at this, and learn from it.
Code:
<html>You should note that document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. Prompt() is also a primitive method of getting data from the user. I find that there is a bug in IE which prevents .innerHTML text being displayed in a <span> while a prompt box is active. As Logic Ali says, you should use your error console to identify syntax errors. "A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman |
Ah. Thank you. This helped a lot, and I see where I was making a lot of mistakes. I am new to this, so your insight is much appreciated.
I was messing around with it, and I do have a few questions: Say the user guessed correctly in their 1st or 2nd attempt, how would you get it to write, for example, "You guessed correctly on your 2nd attempt?" And for that matter, would there be a way to to keep track of their previous guesses in the inner HTML? Thank you. |
Quote:
Code:
if (guess == theNumber) { |
Thank you very much for the help.
I always at the very least attempt my homework. This was an extra "see if you can" assignment, and I just couldn't get it. I am not very skilled at this, but I try, and I do want to learn. What you have shown me has helped me tremendously. Thanks again! |
Haha.
How in the world did you know to put the i = tries after the message ="". If its flipped, it always says 4th attempt. Why does that change make a difference, if you don't mind my barrage of questions? |
Quote:
If i = tries is placed before the message then i takes the value of 4. So it will always say fourth attempt. Note that tries is the number of tries allowed (a constant - 4), not the number of tries made so far which is the value of the variable i. |
Quote:
Quote:
This example tells the user if each guess if too high/low or correct and on which guess without using arrays. Code:
<p>Previous guesses:</p> |
The OP asked for "You guessed correctly on your 2nd attempt". In other words, ordinal numbers. You code gives cardinal numbers, so not a correct solution.
|
Quote:
You can create ordinal numbers without an array. |
Quote:
|
Quote:
Can you post a link to a post where you say I said that. |
Quote:
|
| All times are GMT +1. The time now is 01:49 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.