View Single Post
Old 11-07-2012, 12:57 PM   PM User | #1
cansat
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
cansat is an unknown quantity at this point
JS help with loop

Hi everyone,

I read around the forum before posting anything, and I seemed to get some answers that I needed for other assignments. I guess I should start with a short intro.

I am in a Networking program in College and am taking a intro to Javascript class. I am struggling a bit, but have asked questions elsewhere and have learned quite a bit from taking notes. It seems dificult at first, but when I look over something that works - it excites me and I learn how to do it without needing the codes on a piece of paper in front of me.

So for the code I need help with now, its the loop that I am having problems with. I have to use 'while loop' but unfortunately my text book is limited in explaining how to properly use it. The code below is what I have.

Essentially its a hi-lo game. I need to be able to let the user choose a number between 1-1000 and the game (which will choose the random number using Math.random) will let the user know if they have to go higher or lower.

I can get higher to work, but as soon as lower is prompted and entered, it ends the script. I think its because I have no loop, so its just ending the page as it stands. If anyone can advise me on how to get the while loop to work, that would be excellent!

I do appreciate all the work anyone can do to assist me!

Code:
<script type="text/javascript">

	var gameNumber = Math.floor(Math.random() * 1000) + 1;
		var Numberguess = 0;
		Numberguess ++;
			var guess;
				while (isNaN(guess) | guess < 1 || guess > 1000){
					alert ("Numbers must be within 1 to 1000");
						var guess = prompt("Guess a number between 1 and 1000");	
							
							if(guess < gameNumber) {
							guess = prompt("Enter a higher number");	
							} while ( guess < gameNumber ){
							guess++;
							}
							
							
							if(guess > gameNumber) {
							guess = prompt("Enter a lower number");
							} while ( guess < gameNumber ){
							guess++;
							}
							
						}
	
</script>
cansat is offline   Reply With Quote