Forum Home
Press F1
 
Thread ID: 48332 2004-08-20 05:49:00 Need help real fast: Javascript willie_M (5608) Press F1
Post ID Timestamp Content User
263516 2004-08-22 14:24:00 (70) You guessed it!
(You entered 44


That ain't right...

sorry about making the page so wide.. should've shortened that line..
willie_M (5608)
263517 2004-08-23 02:57:00 I can see many mistakes in that script, and I don't have much knowledge in JavaScript.

First of all is if(statements) having a semi-colon after it, which means the { } brackets are executed as ordinary statements, not confined to the conditions of that if(statement) even if true or false, it wouldn't matter, those lines follow before the next conditional statements and because of the break; it'll never run the next lines. That is why every number guessed would seem correct, even if it isn't.

In your code you set the numlo and numhi number to the usrnum that was guessed, and then afterwards you query the line with if(numlo <= usrnum) { do this }; well since you set usrnum to numlo/numhi guess what will happen? It'll do what you said for it to do and that will become annoying, you really need to call/test it outside the condition.
chameleon_coder (6050)
263518 2004-08-23 03:15:00 Obviously you need a prettyprint programme. I got spoiled by them when using Pascal and Algol. A prettyprint is quicker than a compiler for sorting out block structures ... ifr the last line isn't back to the lefthand side there's a mismatch in the blocks. Similarly, Willie's null for loop would have caused the "body" to be not indented.

Error messages usually mean something. They don't always point at the actual wrong item ... they just show where the system found out. :D
Graham L (2)
263519 2004-08-23 04:14:00 Here's a script I made based off your script and using my knowledge in C/C++, as well as additional help from various websites as I knew JavaScript wouldn't have the same named functions I use in C/C++ but are very similar.


<script language="javascript">

document.write("<button type='button' onClick='play_game();'>Play Game</button> ");

function play_game()
{
var numhi = 100, numlo = 0, randnum, usrnum = -1, counter = 1;

randnum = Math.round((numhi - numlo) * Math.random() + 1);

do
{
usrnum = Number(prompt("Guess a Number Game", "Guess a number between 0 and 100"));

if(usrnum > numhi || usrnum < numlo)
{
alert("Follow Directions! ");
}

counter++

if(isNaN(usrnum) == true || counter > 100)
{
break;
}

if(usrnum != randnum)
{
if(usrnum > randnum)
{
numhi = usrnum;
document.write("You guessed "+ usrnum +". That was too HIGH.
");
}
else
{
numlo = usrnum;
document.write("You guessed "+ usrnum +". That was too LOW.
");
}
}
} while(usrnum != randnum);

if(usrnum == randnum)
{
document.write("<h4>"+randnum+" You guessed it!</h4> You entered "+usrnum+"
");
}
else
{
document.write("Stopped

Either no number was entered, the game took too long, or you gave up</p> ");
}
}
</script>


There's a few things different, like initialising variables when they are made, although this way may not be recommended as it's found quite messy, I just did it that way to speed things up.

I changed the randnum method, only because I couldn't fully understand floor.

I used a do-while loop as it seemed more appropriate, although you could use other loops, I know some programmers rarely use do-while and I could be one.

After the prompt for usrnum, we have the test between numhi/numlo and usrnum, obviously because numhi and numlo are set at 0-100 I could not test whether numhi/numlo == usrnum as we give the person choice of choosing 0 and 100, this will automatically result in executing that block of code, you could fine tune that area if you like, but I thought having a functional program first then ironing out the bugs later.

I then use and incremental counter, I don't believe in having an endless loop, so I made it that if it runs so many loops to break out as well as if the user did not enter a number it would also break out.

The rest should be self explanatory, whether it's high or low or correct, else the game stopped due to some reason.
chameleon_coder (6050)
1 2