/    Sign up×
Community /Pin to ProfileBookmark

Why my last "for" doesn’t work?

Why it doesn’t write me the user guess number to finally say the user tried “x”times to guess the number?

Thanks

<HTML>
<head><title>Javascript Exercices CSPS 130</title></head>
<body>
<h2><u> JavaScript Exercise 10 – RANDOM NUMBER GAME </u> </h2>
<p>
<script text=”javascript”>
// Store the random number that the user has to guess

var my_number = rand ( 100 );

// Start a new game

function new_game ( )
{
// Clear the guess form field
document.getElementById(“user-guess”).value = “”;

// Generate a new random number
my_number = rand ( 100 );

// Tell the user that we’re ready for them to guess
alert ( “OK, I’m thinking of another number…” );
}

// Process the user’s guess

function make_guess ( )
{
// Get the user’s guessed number
var user_guess = document.getElementById(“user-guess”).value;

// Warn if they haven’t entered a number between 1 and 100

if ( isNaN ( user_guess ) || user_guess < 1 || user_guess > 100 )
{
alert ( “Please enter a guess between 1 and 100” );
return;
}

// Compare the guessed number against the computer’s number,
// and respond accordingly

if ( user_guess > my_number )
{
alert ( “Too high – try again!” );
}
else if ( user_guess < my_number )
{
alert ( “Too low – guess a higher number!” );
}
else
{
alert ( “You got it! My number was ” + my_number );
new_game ( );
}
}
[B]for (var user_guess){
document.write (“<br>” user_guess “</br>);

if(my_number) {
break;
}[/B]
}

</script>
<center>
<input id=”user-guess” type=”text” size=”2″ value=”” />
<! button that the user can click to check their guess:>

<input type=”button” value=”Guess” onclick=”make_guess()” />
</center>

</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKEROct 17.2012 — Couple of problems with your highlighted code:
for (var user_guess){
document.write ("&lt;br&gt;" user_guess "&lt;/br&gt;);

if(my_number) {
break;
}

You cannot use document.write anytime after the page has been initially rendered.

Doing so causes the page to reload as if you had entered for the first time ever.

Also your syntax for the string to display is invalid: Should be "<br>"+user_guess+"<br>" if you decide to display it elsewhere.

Other problems exist as well. You should use the error console if you are testing with FF or Chrome browsers.

Finally, you should enclose you script between [ code] and [ /code] tags (without the spaces)

to make it easier for forum members to read, copy, test and retain proper formatting.

?
×

Success!

Help @hectorespresati 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.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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