| 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 | ||
| 263506 | 2004-08-20 05:49:00 | 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 (5608) | ||
| 263507 | 2004-08-20 05:58:00 | too late now... However I would still be interested in why it kicked the bucket.... |
willie_M (5608) | ||
| 263508 | 2004-08-20 08:21:00 | 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. |
bmason (508) | ||
| 263509 | 2004-08-20 11:13:00 | thanks b... too bad it wasn't earlier but oh well its only worth like 4% cheers willie_M |
willie_M (5608) | ||
| 263510 | 2004-08-20 16:06:00 | 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 (6050) | ||
| 263511 | 2004-08-20 16:25:00 | 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. |
chameleon_coder (6050) | ||
| 263512 | 2004-08-21 07:56:00 | > 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. |
bmason (508) | ||
| 263513 | 2004-08-21 09:46:00 | >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. |
willie_M (5608) | ||
| 263514 | 2004-08-21 11:30:00 | did you get it in ??? <emotions not included> | Earnie Moore (5918) | ||
| 263515 | 2004-08-22 14:23:00 | 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 (5608) | ||
| 1 2 | |||||