PDA

View Full Version : Need help real fast: Javascript



willie_M
20-08-2004, 05:49 PM
I got an assignment that needs to be handed in by 5pm!

And this stoopid javascript is throwing a fit about the break command being outside the loop when clearly it is not!

Heres the code:

for(counter=1; counter==1; counter);
{
usrnum=Number(prompt("Guess a number","Guess a number between 0 and 100"));

if(usrnum==randnum);
{
document.write("<h4>You guessed it!</h4> (You entered "+usrnum+"
");
break;//break is inside the loop! WTF! Why does it throw an error!!!
}
if(usrnum<randnum);
{
numlo=usrnum;
document.write("You guessed "+usrnum+". That was too LOW.
");
if (usrnum<=numlo);
{
alert("Follow Directions!");
}
}
if(usrnum>randnum);
{
numhi=usrnum;
document.write("You guessed "+usrnum+". That was too HIGH.
");
if (usrnum>=numhi);
{
alert("Follow Directions!");
}
}
if(usrnum==quit);
{
document.write("Stopped");
break;//break is inside the loop! WTF! Why does it throw an error!!!
}

Please answer real real fast!

willie_M

willie_M
20-08-2004, 05:58 PM
too late now...

However I would still be interested in why it kicked the bucket....

bmason
20-08-2004, 08:21 PM
The problem is right here:
> for(counter=1; counter==1; counter);

If you remove the semi-colon at the end of this line is should work. The semi-colon closes the loop, so the code inside of the { } is just a normal block that will get executed once. So the complaint about the breaks is correct because it is not inside of a loop.

Hopefully who ever is marking the assignment will be kind enough to remove the semi-colon for you.


The usage of "counter" is also a bit dodgy but I don't know what you were trying to do with it.

willie_M
20-08-2004, 11:13 PM
thanks b...

too bad it wasn't earlier but oh well its only worth like 4%

cheers
willie_M

chameleon_coder
21-08-2004, 04:06 AM
bmason,

Looking at the javascript and being similar to what languages I'm use to

for(counter=1; counter==1; counter) or in C/C++ terms for(int counter = 1; counter == 1; counter)

basically it is a endless loop till the randnum is guessed, then it's broken out of the loop.

You could do it like
for(;;)
which is a C/C++ method, or
while(true)
it seems the conversion over to javascript is not that much different to C/C++.

I don't know about the person marking the assignment would be that kind, functions and loops are among the basics that are taught in any language, so it's slim chance that they would go easy, if it were more complex it may have been eased a little.

chameleon_coder
21-08-2004, 04:25 AM
willie M,

You may had the loop closed but look at your script, the last part is missing } for the loop and not the if statement but again, that could have been dropped from the copy and paste.

if(statements){} don't require semi-colons after the parenthesis (), the only time you would put a semi-colon after it is when calling a function, e.g. rand(); Loops don't require a semi-colon after the parentheses for(counter = 1; counter==1; counter){}

If you enjoyed doing this, why not try improving your script? Some suggestions maybe a counter that keep tracks of guesses made, making sure that numbers only are entered in the script and are within the calculated range of 0-100 by randnum, although that part of the script is missing so it could be there. Maybe using else and else if statements.

bmason
21-08-2004, 07:56 PM
> I don't know about the person marking the assignment
> would be that kind, functions and loops are among the
> basics that are taught in any language, so it's slim
> chance that they would go easy, if it were more
> complex it may have been eased a little.

It depends on the marker. I had a CompSci lecturer who was patient enough to try and fix students programs. Even for 3rd years who made the same mistake as above.

willie_M
21-08-2004, 09:46 PM
>the last part is missing }

nah yep, i missed that from the copy and paste... oops.

Dammit why didn't i do a while statement....

that would have solved everything.

Earnie Moore
21-08-2004, 11:30 PM
did you get it in ??? <emotions not included>

willie_M
23-08-2004, 02:23 AM
I screwed it up anyway... dunno how... everytime I play it says you guessed it even tho the random number is different to wat was inputed...

Heres the code if you wanna copy paste (i wont give you an html link for security reasons)

<script language = "Javascript">

/* Robert McLeod: 0468755
Assignment Program - Week 4
Due Friday 20 Aug 2004 Semester 2
program4.html
*/

document.write("<center><h2>Number Guess Game</h2></center>");
document.write("<center>Click on the 'Play Game' button to start playing the game. Then enter a number between 0 and 100 and keep guessing from the hints you recieve until you guess the number correctly");
document.write("
To quit simply type 'quit' in the box and the game will be stopped
");

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

</center>");

function play_game()
{
var numlo, numhi, randnum, usrnum, counter;

randnum = Math.floor(Math.random()*100+1);
numlo=0;
numhi=100;

for(counter=1; counter==1; counter)
{
usrnum=Number(prompt("Guess a number","Guess a number between 0 and 100"));

if(usrnum==randnum);
{
document.write("<h4>"+randnum+"You guessed it!</h4> (You entered "+usrnum+"
");
break;//break is inside the loop! WTF! Why does it throw an error!!!
}
if(usrnum<randnum);
{
numlo=usrnum;
document.write("You guessed "+usrnum+". That was too LOW.
");
if (usrnum<=numlo);
{
alert("Follow Directions!");
}
}
if(usrnum>randnum);
{
numhi=usrnum;
document.write("You guessed "+usrnum+". That was too HIGH.
");
if (usrnum>=numhi);
{
alert("Follow Directions!");
}
}
if(usrnum==quit);
{
document.write("Stopped");
break;//break is inside the loop! WTF! Why does it throw an error!!!
}
}
}
</script>

Should've used a while statement....

willie_M
23-08-2004, 02:24 AM
(70) You guessed it!
(You entered 44


That ain't right...

sorry about making the page so wide.. should've shortened that line..

chameleon_coder
23-08-2004, 02:57 PM
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.

Graham L
23-08-2004, 03:15 PM
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

chameleon_coder
23-08-2004, 04:14 PM
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.