More help need (Javascript this time)

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!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">
<head>
<title>Learning to Multiply Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type = "text/css">
   body { background-color: #ddd }
</style>
<script language = "JavaScript" type="text/javascript">
 var x,y;

 function aNumber() {
      return Math.round(Math.random()*(11)+1);
 }

 function genQuestion() {
      dispQ = document.getElementById("questionVal");
      x = aNumber();
      y = aNumber();
      dispQ.value = "What is " + x + " times " + y + " ?";
 }

function randomCorrectRes(){
               respNumber = Math.round(Math.random()*(1)+1);
               switch(respNumber)
               {
               case 1:
               return "Correct";

               case 2:
               return "Good Answer!";
               }
}

function randomIncorrectRes(){
               respNumber = Math.round(Math.random()*(1)+1);
               switch(respNumber)
{
               case 1:
                       return "No. Please try again.";

               case 2:
               return "Wrong. Try once more.";
               }
}

 function buttonPressed() {
     answer = document.getElementById("answerVal");
     if(answer.value ==""){
         window.alert("Null Value!");
         }else if(answer.value == (x*y)){
     window.alert(randomCorrectRes());
     genQuestion();
     }else{
     window.alert(randomIncorrectRes());
         }
       answer.value="";
       }
</script>
</head>

<body>
<h1>Learning to Multiply Website</h1>

<form name="MyForm" id="MyForm" action="">

<table border = "1">
  <tr>
    <td><b>What is</b></td>
    <td><input id="questionVal" name="question" type="text" /></td>
  </tr>
  <tr>
    <td><b>The answer is</b></td>
    <td><input id="answerVal" name="answer" type="text" /></td>
  </tr>
  <tr>
    <td><input value="" name="checkanswer" type="button"
         onclick="buttonPressed()" /></td>
  </tr>
</table>

</body>
</html>

Basically, i need to fill in the blanks in the table so 'What is' generates the question using the genQuestion function.
'The answer is' You enter manually.
The button should check the answer using the buttonPressed function, and give you a correct or incorrect popup using the randomIncorrectRes and randomCorrectRes functions.

I'm stuck.

Help pwease :)
 

ST^

Can't get enough of FH
Joined
Dec 22, 2003
Messages
2,351
Works for me IF you add:

onload="javascript: genQuestion();"

to the body tag.
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Simple as that! Cheers ST, cookie in the post.
 

megadave

I am a FH squatter
Joined
Apr 3, 2006
Messages
11,911
any idea why it works in firefox and not in internet explorer?
 

ST^

Can't get enough of FH
Joined
Dec 22, 2003
Messages
2,351
It works in IE. The problem is most likely that in winXP, IE security settings for files on the local machine are stricter than when online. If you upload it to a webserver it'll surely work fine.
 

megadave

I am a FH squatter
Joined
Apr 3, 2006
Messages
11,911
Ok thanks, pretty sure we both passed our course anyway so i'll take your word for it :)
 

Users who are viewing this thread

Top Bottom