...

View Full Version : Resolved Guessing Game Help.



Jamesc
12-14-2011, 08:22 PM
What I'm trying to achieve is it initialises a number between 1-2000 randomly.
When the submit button is clicked it simply returns if the number that is in putted is higher or lower than the randomly initialised number.
External JavaScript:

Html


<html>
<head>
<script type="text/javascript" src="jsscri.js"></script>
</head>
<body onload="initialisation ()">

<form name="exampleform">
<h3>
Number: <input name="number" type="text"
onchange='checkNumber( this.value );'>
</h3>
<p>
<input name="submit" type="button" value="Guess"
onclick= 'checkValue()';/>
</p>


</body>
</html>


If you load this onto a webpage you'll see what i'm trying to do. I tried deleting the rest of the code to keep the size of the code down.

mvmacd
12-14-2011, 08:34 PM
"checkNumber" is not a function. Did you leave some code out?

Also, you misspelled initialization as "initialisation" in the onload.

Old Pedant
12-14-2011, 08:41 PM
Also, the checkValue( ) function requires a single argument passed to it.

When invoked from the "Guess" button, no value is supplied.

Jamesc
12-14-2011, 08:42 PM
Sorry. I changed the spelling. Heres the check function. It checks the in-putted text and alerts the user if the number they put is outside the range of 0-2000.



function checkNumber(num)
{

if ( isNaN(num) || num < 0 || num > 2000) {
alert( num + " Number has to be between 0 - 2000");
}
}



Also, the checkValue( ) function requires a single argument passed to it.



<p>
<input name="submit" type="button" value="Guess"
onclick= 'checkValue(num)';/>
</p>


Is this right? I'm not sure if it looks at the values in the input box then checks.

Old Pedant
12-14-2011, 09:13 PM
How could num possibly be right?

Look here:


<input name="number" ...

So:


<input name="submit" type="button" value="Guess"
onclick="checkValue(this.form.number.value)" />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum