/    Sign up×
Community /Pin to ProfileBookmark

creating guessing game

hi there,
im trying to create a simple guessing game which uses functions and buttons. the computer is to create a random number in between two user set limits from the text boxes in the form. that is the role of one function. the second function will process the guesses, ask user for guesses. the game will essentially go on and on until the user says cancel in the confirm box. the third function will only relate the play button with the play function. this is what i have so far.

<body>
<form action=”” method=”post”>
<h1>Guessing Game</h1>
<p>Lower Limit:
<input type=”text” id=”lowerLimit” value=”1″ /><br /><br />
</p>

<p>Upper Limit:
<input type=”text” id=”upperLimit” value=”3″ /><br /><br />
</p>

<p>
<input type=”button” id=”PlayGame” value=”Play Game” onclick= “playGame()” />
</p>

<p>
<input type=”reset” id=”ResetValues” value=”Reset Values” />

</p>

</form>
<script type=”text/javascript”>
/*<![CDATA[*/

/* calling main */
main();

function getRandomValue(lowerLimit, upperLimit) {
document.getElementById(lowerLimit).value;
document.getElementById(upperLimit).value;
var randomNumber = Math.floor((upperLimit – lowerLimit + 1) * Math.random() + lowerLimit);
return randomNumber;
}

function playGame() {
randomNumber;
var guess = Number(prompt(“guess number”));

}

function main() {
/* Where we call the other functions */
playGame()

}

/*]]>*/
</script>

</body>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@omnicityMar 18.2009 — JavaScript isn't Java, so you don't need a main() function.

The rest looks okay so far. When getting items from a form, it is more usual to access them via the forms[] collection, but going direct with getElementById will work as well.

The first line of playGame() needs to change - probably to:
[CODE]randomNumber = getRandomValue();[/CODE]

The function definition for playGame() does not need any parameters as you have things just now.
×

Success!

Help @mattyb141 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.20,
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,
)...