...

View Full Version : JS help with loop



cansat
11-07-2012, 12:57 PM
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!




<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>

Philip M
11-07-2012, 04:18 PM
Here you are. Please study and learn from it.



<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

cansat
11-08-2012, 01:28 AM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum