PDA

View Full Version : need help with guess this number program

alesv
10-14-2007, 12:14 AM
I need help with this Guuess a number program> Its probly easy for for you but I'm brand new at this javascript stuff. Antways here what i got make work and what I have got so far.

Guess a number program- The user will pick a number from 1 to 1000. Your program should guess the user number in ten tries or less.After each guess the user will specify if the guess is low or high or correct.

This what I have so far.

<html>
<head></head>
<title>Guess your interger</title>

<script language = "javascript">

function ShowValues{

Var highnum=1000;
var lownum=0;
var guessnum =(lownum+highnum)/2;
var count=0;

if (document.myform.High.Checked)
{highnum=guessnum}

if (document.myform.Low.Checked){
lownum=guess}

guessnum=(lownum+highnum)/2
count++

myform.guessbox.value=guess
myform.counter.value=counter

}

</script>

<body>

<H1><bold><center> Guess Your Integer</h1>
<h3> Think of an interger between 1and 1000(Don't tell anyone!)<br>

The computer will guess you number in ten guesses or less<br><br><br>
Check high checkbox if our guess was to high
<br>
Check Low checkbox if our guess was to low
<br>
Press Guess button to start game.
<br>
<br>

<br>

<Form name = "myform">

<input type="button" value="Guess"onclick="ShowValues()">
<p>Low<INPUT TYPE = "Checkbox" name = "Low">
<p>High<INPUT TYPE = "Checkbox" name = "High">
<p>Our guess is<input name = "guessbox" type="text">
<p>We have made<input name= "counter" size="8">Guesses

</form>
</head>
</body>

Trinithis
10-14-2007, 12:33 AM
To generate a random number, use Math.random()

Math.random() creates a number from 0 (inclusive) to 1 (exclusive). Symbolically, the range is [0, 1)

Math.ceil(number) rounds a number up to the nearest integer.

Math.floor(number) rounds a number down.

var num = Math.ceil( Math.random() * 1000 ); //A random integer within [1, 1000]

----

About your HTML...
-The title tag belongs within the head tag.
-Your script tag should look like
<script type="text/javascript">
-The script tag should either be within a head or a body tag.
-You have a </head> at the end of your document that should not be there.
-You should close your html tag at the end of your document too.

Twey
10-14-2007, 04:21 PM
It's been long enough that you've probably done it by now anyway, so I guess it's OK for me to show off:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Number Guesser</title>
<script type="text/javascript">
var Functional = {
Operator: {
add: function(a, b) {
return a + b;
}
},
reduce: function(f, a, initial) {
var t = initial || 0;
for(var i = 0; i < a.length; ++i)
t = f(t, a[i]);
return t;
}
};

Math.mean = function() {
return Functional.reduce(Functional.Operator.add, arguments) / arguments.length;
};

function GuessGame(upperBound, lowerBound, maxGuesses) {
this.ub = upperBound || 1001;
this.lb = lowerBound || 1;
this.mg = maxGuesses || 1000;
this.breakPlay = false;
}

GuessGame.HIGHER = 1;
GuessGame.LOWER = 2;
GuessGame.CORRECT = 3;

GuessGame.prototype = {
playBlocking: function() {
var d;

for(var i = 0; i < this.mg; ++i)
if(!this.breakPlay && (d = this.tryGuessBlocking()))
this.updateBounds(this.getGuess(), d);
else break;

if(this.breakPlay && this.ub !== this.lb)
this.fail();
},
getGuess: function() {
return Math.floor(Math.mean(this.ub, this.lb));
},
updateBounds: function(num, direction) {
if(direction === GuessGame.HIGHER)
if(num < this.ub)
return (this.ub = num) ? true : true;
else {
alert(num + " < " + this.ub);
return false;
}
else if(direction === GuessGame.LOWER)
if(num > this.lb)
return (this.lb = num) ? true : true;
else return false;
else if(direction === GuessGame.CORRECT) {
this.succeed(num);
return (this.ub = this.lb = num) ? true : true;
}
},
tryGuessBlocking: function() {
var h;
do
h = (h = prompt("I guess " + this.getGuess() + ". Is this [h]igher, [l]ower, or [c]orrect?")) ? h.charAt(0).toLowerCase() : h;
while(h !== null && (h.length !== 1 || "hlc".indexOf(h) === -1));

if(h === null)
return null;
else return ({h: GuessGame.HIGHER,
l: GuessGame.LOWER,
c: GuessGame.CORRECT})[h];
},
succeed: function(n) {
this.breakPlay = true;
alert("The answer is " + n);
},
fail: function() {
this.breakPlay = true;
alert("I failed to find your answer.");
}
};
</script>
</head>
<body>
<div>
<input type="button" onclick="(new GuessGame()).playBlocking();" value="Play">
</div>
</body>
</html>

jscheuer1
10-14-2007, 04:59 PM
It's been long enough that you've probably done it by now anyway, so I guess it's OK for me to show off:

Long enough? It was only yesterday! He/She probably hasn't even cracked the book yet.

Twey
10-14-2007, 06:30 PM
Well, considering that it was probably last minute before s/he asked anyway...

jscheuer1
10-14-2007, 06:51 PM
Offhand, I'm not really sure exactly what time of day Sunday it is over there, later I know. But when you posted your solution it was well before late Sunday night, the time when all true homework slackers get down to it, if they are going to do it at all. Over here, there's still time for a pick up game of basketball, or whatever before anyone needs to worry seriously about homework. :p

Twey
10-14-2007, 06:58 PM
Heh, fair enough :) I won't take it down now since I'd just lose it. I suspect the teachers would be suspicious if s/he handed in my work anyway, though, judging by the previous attempt.

alesv
10-14-2007, 09:03 PM
To be honest I'm not a homework slacker this assignment was already turn in as is I got a 80 for trying and at least getting some things right. However my professor doesn't tell us what we did wrong and I personaly want to get this program to work.
Thanks again for the Help
Vinnie

jscheuer1
10-14-2007, 10:47 PM
my professor doesn't tell us what we did wrong

Bad professor, bad, bad . . .