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 09-09-2012, 09:31 PM   PM User | #1
roosnanie
New to the CF scene

 
Join Date: Sep 2012
Location: Atlanta, ga
Posts: 4
Thanks: 4
Thanked 0 Times in 0 Posts
roosnanie is an unknown quantity at this point
Help with a while loop

Im in my first scripting class, I am supposed to code a number guessing game with a while loop that asks the user if they want to play the game..
this is what I have so far..
what do I need to do ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Hot or cold Guessing Game</title>

</head>
<body>

<script type="text/javascript">
/*
The code below will pop up an alert box
and ask you to guess the number, it will then
let user know if hot or cold and when you win it
will calculate the number of guesses it took the user to get to the answer
*/
var random_number = parseInt(Math.random() * 100, 10);
var guesses = 0;
var last_guess = 0;
var message = "";
var user_response ="yes";

while (user_response !="yes"){
user_response = prompt ("Do you want to play again?");
if (user_response == "yes") { HAVING A PROBLEM HERE !=CONTINUE GAME;
{ else if (user_response=="") { End Game Here ;
}



while (last_guess != random_number) {
guesses++;
if (last_guess == 0) >= (random_number - 20) && last_guess <= (random_number + 20)) {
message = "You're getting cold.";
} else {
message = "Ice Cold!";
}
last_guess = parseInt("0" + prompt(message, last_guess), 10);
}

message = "Success! You Win!\n";
if (guesses < 5) {
message += "You did an awesome job, it only took you " + guesses + " tries to guess the number!";
} else if (guesses <= 15) {
message += "Goodness. It took you " + guesses + " tries to guess the target number.";
} else {
message += "Yikes! It took you " + guesses + " tries to guess the target number.";
}
alert(message);
{

} // end if
} // end while
//]]>
</script>

</body>
</html>
roosnanie is offline   Reply With Quote
Old 09-10-2012, 03:21 PM   PM User | #2
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
Code:
var user_response ="yes";
while (user_response !="yes"){
 	user_response = prompt ("Do you want to play again?");
	if (user_response == "yes") { HAVING A PROBLEM HERE !=CONTINUE GAME;
	{ else if (user_response=="") { End Game Here ; }
Simple answer is you just jump out while loop before it start.
To fix this problem is just change "yes" at var user_response to "no" or something else except "yes" and "".
Richter 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 04:55 AM.


Advertisement
Log in to turn off these ads.