Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Javascript guessing game problem?
I am trying to create a random number guessing game in Javascript and having problems with my code. I cant work out exactly what the problem is though the random number is fine and the transfer of the value the user enters into the form is fine. Its somewhere in the function process that the error lies.
Firebug keeps telling me there is various things undefined and so on. So I try to define them and it moves onto a new one etc.
<html lang="en">
<head>
<title>JS Guessing Game</title>
<script type="text/javascript">
function createRandomNumber() {
// this generates a random number between 0 and 100, then hands it back
// to the code that called the function.
return Math.floor(Math.random()*101);
}
var randnumb = createRandomNumber(); // gets the random number and stores it.
alert(randnumb);
function process() {
alert(document.forms.formguess.guess.value);
var guess = document.forms.formguess.guess.value; // transfers the value of guess textbox to userguess (within the function)
document.forms.formguess.guess.value=''; // clears the guess box.
//return userguess;
}
var result = process();
if (randnumb == result)
{
alert("Correct");
}
if (randnumb<result)
{
alert("less than");
}
if (randnum>result)
{
alert("greater than");
}
</script>
</head>
<body>
<h1>JavaScript Number Guessing Game</h1>
<form onSubmit="" name="formguess">
<input type="text" name="guess" value="">
Take a Guess
<input type="button" value="Guess" onClick='process(randnumb)'>
</form>
</body>
</html>
Thanks in advance for your help.