Please support our HTML and CSS advertiser: Lunarpages Web Hosting
Views: 8011 | Replies: 14
![]() |
<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">
function operator(op){
if(op==0)
return "+"
if(op==1)
return "-"
if(op==2)
return "x"
else
return "/"
}
function add(n1, n2, n3, n4){
var ans
if(n3==0){
ans=n1+n2
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans)
}
if(n3==1){
ans=n1-n2
if(ans==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans)
}
if(n3==2){
ans=n1*n2
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans)
}
if(n3==3){
ans=n1/n2
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans)
}
}
</script>
</head>
<body>
<font color="black" size="7"><center>
<script type="text/javascript">
var num1=(Math.floor(Math.random()*11))
var num2=(Math.floor(Math.random()*11))
var operation=(Math.floor(Math.random()*3))
var convertedOp=operator(operation)
document.write(num1 + "<br>" + convertedOp+ " <br>" + num2 + "<br>")
document.write("______")
var answer=document.form1.tfield.value;
function check(){
add(num1,num2,operation,answer)
}
</script>
<form action="test.html" name="pormas">
<input type="text" size=3 name="tfield" id="ans" value=0><br>
<input type="button" value="Answer" name="ans" onclick="return check()">
</form>
</body>
</html> Last edited by tgreer : Aug 3rd, 2006 at 11:29 am.
if you cant hit 2 birds with one stone try hitting just one or you will end up with nothing at all
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
Number one READ what we are telling you.
1. PUT ; at the end of your statements.
2. replace
Now this line
1. PUT ; at the end of your statements.
2. replace
var answer=document.form1.tfield.value; with what we told you THREE times now var answer=document.getElementById['tfield'].valueNow this line
var answer=document.form1.tfield.value; is not only syntacticly wrong, it's in the wrong place. It will get parsed and executed by the browser BEFORE the text box "tfield" exists in the DOM (Document Object Model) so instead you need to move it into function Check() •
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
And you have this bit wrong
You can't reference the textbox by name, you must use the ID.
this code works
<input type="text" size=3 name="tfield" id="ans" value=0> it should be <input type="text" size=3 name="tfield" id="tfield" value=0>You can't reference the textbox by name, you must use the ID.
this code works
<html>
<head>
<title>
hi!!!
</title>
<script type="text/javascript">
function operator(op){
if(op==0)
return "+";
if(op==1)
return "-";
if(op==2)
return "x";
else
return "/";
}
function add(n1, n2, n3, n4){
var ans;
if(n3==0){
ans=n1+n2;
if(ans ==n4)
alert("you got it right!");
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==1){
ans=n1-n2;
if(ans==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
if(n3==2){
ans=n1*n2 ;
if(ans ==n4)
alert("you got it right!")
else
alert("Sorry its wrong. the answer is: "+ans) ;
}
if(n3==3){
ans=n1/n2 ;
if(ans ==n4)
alert("you got it right!") ;
else
alert("Sorry its wrong. the answer is: "+ans);
}
}
</script>
</head>
<body>
<font color="black" size="7"><center>
<script type="text/javascript">
var num1=(Math.floor(Math.random()*11));
var num2=(Math.floor(Math.random()*11));
var operation=(Math.floor(Math.random()*3));
var convertedOp=operator(operation);
document.write(num1 + "<br>" + convertedOp+ " <br>" + num2 + "<br>");
document.write("______");
function check(){
var answer=document.getElementById("tfield").value;
add(num1,num2,operation,answer) ;
}
</script>
<form action="test.html" name="pormas">
<input type="text" size=3 name="tfield" id="tfield" value=0><br>
<input type="button" value="Answer" name="ans" onclick="return check()">
</form>
</body>
</html> Last edited by hollystyles : Aug 3rd, 2006 at 7:08 am.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode