With this program, if the user gets the answer correct they will get a alert saying good. However I need an prompt to appear right after the alert asking the user if they would like to answer another questions. which the user will type yes or no. Yes, it will execute again, no it will end. (Posted a thread before on this but it was resolved, this is a new problem)
Any help on this would be great!
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>DS</title>
<script LANGUAGE = "JavaScript">
function begin()
{ var value, value2, answer, input;
x = functionValue();
y = functionValue();
answer = x * y;
while(input!=answer){
//Get user input
input = parseFloat(window.prompt("How much is "+x+" times "+y+"?", "0"));
if(input==answer){
alert("Good!");}
else
alert("Wrong");}
}
function functionValue()
{
return Math.floor( 1 + Math.random() * 9 );
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="begin()">
</BODY>
</HTML>
Last edited by Kor; 03-19-2009 at 08:50 AM.
Reason: wrap the code [code][/code]
Two versions for you (I personally like the confirm option better), note that x, y and answer need to be re-defined inside the while loop or it will constantly ask the same exact thing:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>DS</title>
<script type="text/javascript">
function begin() {
var value, value2, answer, input = 'yes', x, y; //input needs a string definition prior to the while loop in this case
while(input.toLowerCase() !== "no"){
//Get user input
x = functionValue();
y = functionValue();
answer = x * y;
input = window.prompt("How much is "+x+" times "+y+"?", "0");
if(Number(input) == answer){
alert("Good!");
} else {
alert("Wrong");
}
input = window.prompt("Would you like to answer another question? (yes or no?)");
}
}
function functionValue(){
return Math.floor( 1 + Math.random() * 9 );
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="begin()"></BODY></HTML>
The following version uses a confirm box instead (a bit tidier than forcing another user input):
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>DS</title>
<script type="text/javascript">
function begin() {
var value, value2, answer, input, x, y, con = true; //con needs pre-definition of true
while(con){
//Get user input
x = functionValue();
y = functionValue();
answer = x * y;
input = window.prompt("How much is "+x+" times "+y+"?", "0");
if(Number(input) == answer){
alert("Good!");
} else {
alert("Wrong");
}
con = confirm("Would you like to answer another question?\n(o.k. to continue or cancel to exit)"); //returns true on o.k. or false on cancel
}
}
function functionValue(){
return Math.floor( 1 + Math.random() * 9 );
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="begin()"></BODY></HTML>
Heres the code that I got from your example, I wanted to know How can I make it so that the user will go back to the same question if they got the answer wrong.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ds</title>
<script type="text/javascript">
function begin() {
var value, value2, answer, input = 'yes', x, y; //input needs a string definition prior to the while loop in this case
while(input.toLowerCase() !== "no"){
//Get user input
x = functionValue();
y = functionValue();
answer = x * y;
input = window.prompt("How much is "+x+" times "+y+"?", "0");
if(Number(input) == answer){
alert("Very Good!");
input = window.prompt("Would you like to answer another question? (yes or no?)");
}
else {
alert("No. Please try again");
}
}
}
function functionValue(){
return Math.floor( 1 + Math.random() * 9 );
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="begin()">
</BODY>
</HTML>
I wanted to know How can I make it so that the user will go back to the same question if they got the answer wrong.
Add in a boolean variable - in this case, newQuestion - and redefine x, y and answer only if newQuestion is true:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ds</title>
<script type="text/javascript">
function begin() {
var value, value2, answer, input = 'yes', x, y, newQuestion = true; //input needs a string definition prior to the while loop in this case
while(input.toLowerCase() !== "no"){
//Get user input
if (newQuestion) {
x = functionValue();
y = functionValue();
answer = x * y;
}
input = window.prompt("How much is "+x+" times "+y+"?", "0");
if(Number(input) == answer){
alert("Very Good!");
input = window.prompt("Would you like to answer another question? (yes or no?)");
newQuestion = true;
} else {
alert("No. Please try again");
newQuestion = false;
}
}
}
function functionValue(){
return Math.floor( 1 + Math.random() * 9 );
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="begin()"></BODY></HTML>
Bookmarks