Beep
07-25-2002, 04:16 PM
New to programming of any sort. I've been self-teaching JavaScript from Andy Harris' JavaScript Programming for the Absolute Beginner. Using his examples, I created a working addition practice script that uses a prompt to ask and answer questions.
I want to make a similar script using text boxes. But my alert for userAnswer != correctAnswer ("Sorry not correct. Try again!") appears immediately on starting the script -- because the answer text box must be (?) initialized empty, yet that calls the alert as I've conditioned it.
Any suggestions on how to escape this Catch-22? Code follows (it doesn't yet supply a new problem if question is answered correctly.) Many thanks.
<html>
<head>
<title>Addition Practice</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>Addition Practice<br>
</h1>
<form method="POST">
<font face="Arial, Helvetica, sans-serif" size="2">
<input type = "button"
name="buttonStart"
value = "Start"
onClick = newProblem();
// runs the function newProblem()
>
</font>
</form>
<script language="JavaScript">
function newProblem()
{
var numA = 0;
var numB = 0;
numA = Math.floor(Math.random() * 100) + 1;
numB = Math.floor(Math.random() * 100) + 1;
var txtQuestion = "";
document.myForm.txtQuestion.value = + numA + " + " + numB;
var correctAnswer = numA + numB;
var userAnswer = document.myform.txtAnswer.value;
if (userAnswer == correctAnswer){
alert("Correct!!!");
} else {
alert ("Sorry not correct. Try again!");
}
}
</script>
<form name="myForm">
<input type=text
value=""
size="18"
name="txtQuestion">
=
<input type=text
value=""
size="8"
name="txtAnswer">
</form>
</center>
</body>
</html>
I want to make a similar script using text boxes. But my alert for userAnswer != correctAnswer ("Sorry not correct. Try again!") appears immediately on starting the script -- because the answer text box must be (?) initialized empty, yet that calls the alert as I've conditioned it.
Any suggestions on how to escape this Catch-22? Code follows (it doesn't yet supply a new problem if question is answered correctly.) Many thanks.
<html>
<head>
<title>Addition Practice</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>Addition Practice<br>
</h1>
<form method="POST">
<font face="Arial, Helvetica, sans-serif" size="2">
<input type = "button"
name="buttonStart"
value = "Start"
onClick = newProblem();
// runs the function newProblem()
>
</font>
</form>
<script language="JavaScript">
function newProblem()
{
var numA = 0;
var numB = 0;
numA = Math.floor(Math.random() * 100) + 1;
numB = Math.floor(Math.random() * 100) + 1;
var txtQuestion = "";
document.myForm.txtQuestion.value = + numA + " + " + numB;
var correctAnswer = numA + numB;
var userAnswer = document.myform.txtAnswer.value;
if (userAnswer == correctAnswer){
alert("Correct!!!");
} else {
alert ("Sorry not correct. Try again!");
}
}
</script>
<form name="myForm">
<input type=text
value=""
size="18"
name="txtQuestion">
=
<input type=text
value=""
size="8"
name="txtAnswer">
</form>
</center>
</body>
</html>