help with a small project

Mcore

Thread Thumper
Joined
Apr 4, 2010
Messages
1,013
So i recently got into Coding in java, and right now im making a simple little program for the sake of practice, but im stuck on something.

<html>
<head>
<script type/"javascript")>
var response = prompt("yes or no?")
if (response === "yes") {
document.write("You Chose yes")
}
else if (response === "no") {
document.write("You Chose no")
}
else{
prompt("Please chose one of the options available. yes or no?")
}
</script>
</head>
</html>

What i want to do is make the else condition loop back to the "if (response...)", but i just cant figure out how to do it without repeating the code a bunch of times. Any help is appreciated
 
What's wrong with running the conditions again below the else prompt... It would only be running it that one time extra.. if they hit cancel it's going to cancel out.. >.> Since you want to reuse the response variable gotta change the output on the variable on the ELSE statement at the end.. voila.

This is me guessing you were speaking about the document write portion and not actually wanting to put the user into a loop. That is a whole nother can of worms :)

<html>
<head>
<script type/"javascript")>

var response = prompt("yes or no?")

if (response === "yes") {
document.write("You Chose yes")
}
else if (response === "no") {
document.write("You Chose no")
}
else {
var response = prompt("Please chose one of the options available. yes or no?")

if (response === "yes") {
document.write("You Chose yes")
}
else if (response === "no") {
document.write("You Chose no")
}
else{
}
}

</script>
</head>
</html>
 
Last edited:
Its been a fair while since I have programmed, but I seem to remember that there is a do while loop you can use to encapsulate that code... -

'I edited my bad coding out....'
 
Its been a fair while since I have programmed, but I seem to remember that there is a do while loop you can use to encapsulate that code... -

'I edited my bad coding out....'

Yeah, just throw it in a while loop.

Code:
<html>
<head>
<script type/"javascript")>
	var done = false;
	while(!done){
		var response = prompt("yes or no?")
		if (response == "yes") {
			document.write("You Chose yes");
			done = true;
		}
		else if (response == "no") {
			document.write("You Chose no");
			done = true;
		}
		else
			alert("Please choose one of the available options.");
	}
</script>
</head>
</html>

By the way, this is JavaScript, not Java. There's a pretty big difference between the two.
 
Last edited:
Yeah, just throw it in a while loop.

Code:
<html>
<head>
<script type/"javascript")>
	var done = false;
	while(!done){
		var response = prompt("yes or no?")
		if (response == "yes") {
			document.write("You Chose yes");
			done = true;
		}
		else if (response == "no") {
			document.write("You Chose no");
			done = true;
		}
		else
			alert("Please choose one of the available options.");
	}
</script>
</head>
</html>

By the way, this is JavaScript, not Java. There's a pretty big difference between the two.

Thanks, i just flat out didn't understand how to negate the while loop, but now that i see its really easy >_> Thanks
 
Thanks, i just flat out didn't understand how to negate the while loop, but now that i see its really easy >_> Thanks

here a really small and simple example I did for ya..


Code:
<html>
<head>
<script type/"javascript")>

  
  while ( prompt('Enter "yes" to continue').toLowerCase()  == "yes" )
    document.write('You Choose yes!<br>')

  document.write('Bye Bye!')


</script>
</head>
</html


the while loop "test" is where or not what you type == "yes".

For kicks and grins, I had it ignore upper case letters.
 
Thats the great thing about coding though.. so many different ways to do it :) Wasn't exactly sure how.... um.. advanced you were in java, hence the code.
 
Back
Top