Forum Moderators: open
After the initial guess I want the second guess to say if you got warmer or colder. So say the number is 50 you guess 75 and it says too high. then you guess 22 I want it to say you are getting colder because your second guess was 28 away instead of 25. And the same with warmer if you next guess is within 25 it should say your getting warmer. I can't figure out how to add that to what I have already.
Plus which is the most important part I need to create an array that will hold all guesses made and I made a button that when pushed it will display those guesses in a div on my html page. I can't figure out how to make an array hols all those guesses.
Here is what I have.
[code]
// Store the random number that the user has to guess
var my_number = rand(1000);
// Start a new game
function newGame ()
{
// Clear the guess form field
document.getElementById("user-guess").value = "";
// Generate a new random number
my_number = rand(1000);
// 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 makeGuess ()
{
// 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 1000
if ( isNaN ( user_guess ) ¦¦ user_guess < 1 ¦¦ user_guess > 1000 )
{
alert ( "Please enter a guess between 1 and 1000" );
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 ( );
}
}
function rand ( n )
{
return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
function getCheckedValue(radioObj){
if (!radioObj)
return "";
var radioLength = radioObj.length;
if (radioLength == undefined)
if (radioObj.checked)
return radioObj.value;
else
return "";
for (var i = 0; i < radioLength; i++) {
if (radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj[i].checked = false;
if(radioObj[i].value == newValue.toString()) {
radioObj[i].checked = true;
}
}
}
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Loan Amortization</title>
<script src="guess.js" language="JavaScript" type="text/javascript"></script>
</script>
<!-- Function to grab values.-->
<script>
function getInt(id) {
return parseInt(document.getElementById(id).value);}
</script>
<script>
function getFloat(id) {
return parseFloat(document.getElementById(id).value);}
</script>
<script>
function dropList(myId)
{
var mylist=document.getElementById(myId);
return mylist.options[mylist.selectedIndex].value;
}
</script>
</head>
<body><form><fieldset><table>
<legend>Inputs</legend>
<table cellpadding="5">
I have a number from 1 to 1000. Can you<br>
guess my number?
<td>Your Guess: <input id="user-guess" type="text" size="6" value="" />
<input type="button" size="6" value="Guess" onclick="makeGuess()" />
</form></td></tr>
<tr>
<td colspan="2"><input type=button onclick="showGuesses('outputDivId', 'guesses[i]')" value="Show My Guesses" />
<input type="button" value="New Game" onClick="newGame()">
</tr
</label>
</table>
<form onsubmit="return false;" action="" method="get" name="radioExampleForm">
<p>
<label for="cheat">
<input id="cheat" type="radio" name="Cheat" value="Cheat"/>
Cheat
</fieldset></form>
</body>
<body><form><table><fieldset>
<legend>Outputs</legend>
<div id="outputDivId" name="output" style="display: block;">Ready to play...</div>
</fieldset></table></form></body></html>
I also have a radio button to display number if the guesser gives up but it is not a problem I can get that part so disregard. I appreciate any help
if ( isNaN ( user_guess ) ¦¦ user_guess < 1 ¦¦ user_guess > 1000 )
{
It may be working OK, but the possibility exists that whatever's on the left of the "or" may get evaluated with what's on the right, like
if ( 1 ¦¦ user_guess )
So it's a good idea to do
if ( isNaN ( user_guess ) ¦¦ (user_guess < 1) ¦¦ (user_guess > 1000) )
{
insuring the conditions are correctly evaluated.
I think all you'll have to do is keep a couple globals handy to compare the previous guesses and previous distances. Still has a couple "oddities" to work out, but note the overall function and message improvement. Working code to get you started, be sure to change these --> ¦ to actual pipe characters, this message board borks them:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- DOCTYPE ALL ON ONE LINE -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
var my_number = rand(1000);
var previous_guess,previous_distance;
// Start a new game
function newGame () {
document.getElementById("user-guess").value = previous_guess = previous_distance = "";
my_number = rand(1000);
alert ( "OK, I'm thinking of another number..." );
}
// Process the user's guess
function makeGuess () {
var dist_descr,distance;
var user_guess = document.getElementById("user-guess").value;
if (isNaN(user_guess) ¦¦ (user_guess < 1) ¦¦ (user_guess > 1000)) {
alert ("Please enter a guess between 1 and 1000.");
document.getElementById("user-guess").value=0;
return;
}
else if (user_guess > my_number) {
if (previous_guess > 0) {
distance = parseInt(user_guess-my_number);
// if you want an alert to see all values, put it HERE
if (previous_distance > 0) {
dist_descr = (previous_distance > distance)?'warmer':'colder';
alert("Still to high, getting " + dist_descr + "!");
}
else { alert ( "Too high - try again!" ); }
previous_guess = user_guess;
previous_distance = distance;
}
else {
previous_guess = user_guess;
previous_distance = parseInt(user_guess-my_number);
alert ("Too high - try again!");
}
}
else if ( user_guess < my_number ) {
if (previous_guess > 0) {
distance = parseInt(my_number-user_guess);
// if you want an alert to see all values, put it HERE
if (previous_distance > 0) {
dist_descr = (previous_distance > distance)?'warmer':'colder';
alert("Still to low, getting " + dist_descr + "!");
}
else { alert ( "Too low - guess a higher number!" ); }
previous_guess = user_guess;
previous_distance = distance;
}
else {
previous_guess = user_guess;
previous_distance = parseInt(my_number-user_guess);
alert ("Too low - guess a higher number!" );
}
}
else {
alert ("You got it! My number was " + my_number+".");
if (confirm('New Game?')) { newGame(); }
document.getElementById("user-guess").value=previous_guess=previous_distance='';
}
} //end func
function rand (n){
return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
</script>
</head>
<body>
<h2>Inputs</h2>
<form action="" onsubmit="return false;">
<p>I have a number from 1 to 1000. Can you guess my number?</p>
<p>
<input type="submit" size="6" value="Start New Game" onclick="newGame()">
<label for="user-guess">Your Guess:</label> <input id="user-guess" type="text" size="6" value="">
<input type="submit" size="6" value="Guess" onclick="makeGuess()">
</p>
</form>
</body>
</html>
The oddity of which I speak: The warmer/colder works OK, but if you say, guess high, then the next guess is low, it will always say "getting warmer/colder" so what you probably need is two sets of "distance" variables, one for above the number, one below. But overall, it works.
.....
else {
previous_guess = user_guess;
previous_distance = parseInt(my_number-user_guess);
alert ("Too low - guess a higher number!" );
addComma=(document.getElementById('write-div').innerHTML=='')?0:1;
if (addComma==1) {
document.getElementById('write-div').innerHTML =
document.getElementById('write-div').innerHTML + ','+user_guess;
}
else {
document.getElementById('write-div').innerHTML=user_guess;
}
}