/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript code..guessing game…

I didnt remember what the document.write thing was for in the for loop and I also wasnt sure what the code would be to get the numbers guessed from the text box to compare them with the random number. I know something else is wrong to, but Im not sure what. Im a total newbie at this stuff. This is what I have so far:

function startGame ( )
{
var ranNum = Math.floor((Math.random()*50)+1);
document.getElementById(“t1”).value = ranNum
var i=0;
for (i=0;i<=10;i++)
{
document.write(“Hello” + i + “<br>”);
}
var answer = prompt(“Guess a number between 1 and 50!”);
if(answer > random)
{
alert(“Guess lower!”);
continue;
}
else if(answer < random)
{
alert(“Guess higher!!”);
continue;
}
else if(answer==random)
{
alert(“Correct!”);
break;
}
}

</script>
</head>
<body>
<form name=”aform” id=”aform”>
<strong> Guess a random number </strong>
<input type=”text” name=”t1″ id=”t1″>
<input type=”button” name=”b1″ id=”b1″ value=”Generate random number”
onclick=”startGame()”>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 19.2012 — You cannot use document.write() after the page has been rendered.

If you try to use it, the script is reloaded and you start all over again.

Essentially you will get ONLY ONE guess at your game, period.

That assumes it would run at all because there is no setting of the variable 'random'

which is what you are trying to compare to with your nested IF statements.

Note the following changes (trying to keep the spirit of your original design)

and compare it to your design. It would also behoove you to use the error console

if you are using the FF or Chrome browsers for testing.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt; JS guessing game &lt;/title&gt;
&lt;script type="text/javascript"&gt;
var ranNum;
function playGame ( ) {
var answer = prompt("Guess a number between 1 and 50!");
if(answer &gt; ranNum) { alert("Guess lower!"); }
if(answer &lt; ranNum) { alert("Guess higher!!"); }
if(answer == ranNum) { alert("Correct!"); return true; }
return false;
}
function generateRandomNumber() {
ranNum = Math.floor(Math.random()*50)+1;
}
window.onload = function() { generateRandomNumber(); }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;strong&gt; Guess a random number &lt;/strong&gt;
&lt;input type="button" value="Play game" onclick="playGame()"&gt;
&lt;input type="button" value="New game" onclick="generateRandomNumber()"&gt;
&lt;/body&gt;
&lt;/html&gt;


Finally you should enclose your scripts between [ code] and [/ code] tags

without the spaces to make it easier to spot your code in the thread.
Copy linkTweet thisAlerts:
@IlovefutbolauthorApr 19.2012 — Thank you so much JMRKER!! I was so confused my teacher made it sound as if we were supposed to have one function, but now I see why I needed to have two which makes total sense. Now how would I do a while loop that lets the user guess 10 times instead of an infinite number of times? I was confused on that too because I dont now if its part of the if statement or not.
Copy linkTweet thisAlerts:
@JMRKERApr 19.2012 — Not the way I would do it, but still trying to keep with your particular program logic...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt; JS guessing game &lt;/title&gt;
&lt;script type="text/javascript"&gt;
var ranNum;
function playGame ( ) {
var answer;
var fnd = false;
var cnt = 10;
while ((cnt &gt; 0) &amp;&amp; (fnd == false)) {
answer = prompt("Guess a number between 1 and 50!");
if (answer &gt; ranNum) { alert("Guess lower!"); }
if (answer &lt; ranNum) { alert("Guess higher!!"); }
if (answer == ranNum) { alert("Correct!"); fnd = true; }
cnt--;
}
if (!fnd) { alert('Too bad, you lose ... The number was '+ranNum); }
return fnd;
}
function generateRandomNumber() {
ranNum = Math.floor(Math.random()*50)+1;
}
window.onload = function() { generateRandomNumber(); }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;strong&gt; Guess a random number &lt;/strong&gt;
&lt;input type="button" value="Play game" onclick="playGame()"&gt;
&lt;input type="button" value="New game" onclick="generateRandomNumber()"&gt;
&lt;/body&gt;
&lt;/html&gt;

Make sure you can explain all the parts to your instructor! :eek:
Copy linkTweet thisAlerts:
@IlovefutbolauthorApr 19.2012 — Thanks again JMRKER! What does fnd stand for? Im guessing cnt stands for count? So for the last statement !fnd. Does that mean that if fnd is true then that is the number that was guessed correctly, but if its false that means that after 10 tries through the loop you didnt guess the correct number? Im not sure what ! means I looked it up and it said that it toggles a statement from false to true or true to false. Sorry Im just trying to make sure I totally understand it.
Copy linkTweet thisAlerts:
@PadonakApr 19.2012 — fnd stands for "found" ever a russian can get it lol )))
Copy linkTweet thisAlerts:
@IlovefutbolauthorApr 19.2012 — Ok thanks! I dont really know anything about javascript Im a total novice. Sorry.
Copy linkTweet thisAlerts:
@JMRKERApr 19.2012 — Thanks again JMRKER! What does fnd stand for? Im guessing cnt stands for count? So for the last statement !fnd. Does that mean that if fnd is true then that is the number that was guessed correctly, but if its false that means that after 10 tries through the loop you didnt guess the correct number? Im not sure what ! means I looked it up and it said that it toggles a statement from false to true or true to false. Sorry Im just trying to make sure I totally understand it.[/QUOTE]

fnd stands for "found" ever a russian can get it lol )))[/QUOTE]

In the words of John Wayne: "Yep".

fnd was abbreviation for found.


In this case, fnd defined as true means found and fnd as false means not-found.

!fnd means false or not-found when the value of fnd is true.

It really gets squirely if you define fnd = false.


Then !fnd would be true, but it makes reading the code a bit confusing.

I suggest using the assignment that makes the most sense to you and stick with it!

?
Copy linkTweet thisAlerts:
@PadonakApr 19.2012 — it's ok i was just trollin <wink>
×

Success!

Help @Ilovefutbol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...