Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 11-07-2012, 04:18 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are. Please study and learn from it.


Code:
<html>
<head>
<body>

<script type = "text/javascript">
var gameNumber = Math.floor(Math.random() * 1000) + 1;
alert (gameNumber);  // for testing
var Numberguess = 0;
var guess = 0;

while (guess != gameNumber) {

if (guess == 0) {
for (var i=1; i<2; i++) {
guess = prompt("Enter a number from 1 - 1000", "");
if ((isNaN(guess)) || (guess == "" || guess < 1 || guess >1000)) {
alert ("You must enter a number between 1 and 1000!!  ");
i -- ;
}
}
Numberguess ++;
}

if (guess < gameNumber) {
for (var i=1; i<2; i++) {
guess = prompt("No - Enter a higher number", "");
if ((isNaN(guess)) || (guess == "" || guess < 1 || guess >1000)) {
alert ("You must enter a number between 1 and 1000!!  ");
i -- ;
}
}
Numberguess ++;
}

if (guess >  gameNumber) {
for (var i=1; i<2; i++) {
guess = prompt("No - Enter a lower number", "");
if ((isNaN(guess)) || (guess == "" || guess < 1 || guess >1000)) {
alert ("You must enter a number between 1 and 1000!!  ");
i -- ;
}
}
Numberguess ++;
}

}

alert ("Well done!  You guessed the number  " + gameNumber + " in " + Numberguess + " guesses");

</script>

</body>
</html>

It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.


It is your responsibility to die() if necessary….. - PHP Manual
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-08-2012, 01:28 AM   PM User | #3
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
Hi Phillip,

Thank you for the response and code. As for your tidbit at the end - I could not agree more!

I also have a networking class that I was somewhat struggling with about a month ago. I got my boss at work (who took the program a few years back) to draw me a diagram of the OSI and TCP/IP layers to better explain it and now I understand better.

I do appreciate your assistance, and I will review the code thoroughly. It would be rather useless to just copy and paste other peoples code because eventually, when I get into the workplace, I am going to need to understand how to do these codes.

I do thank you again for helping me out!
cansat is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:35 AM.


Advertisement
Log in to turn off these ads.