Hello,
I'm very close to finishing this
javaScript code and
HTML, but I've got an annoying problem.
The webpage is supposed to look something like the picture (click the link):
http://www.webdeveloper.com/forum/at...6&d=1197038753
It basically asks the user simple multiplication questions, and when the user types in the answer, it either comes up with an alert window displaying a message like "Correct, well done" or "Sorry, try again".
I have managed to randomly generate the multiplication questions, but pressing the button to check the answer comes up with nothing. It is supposed to display randomly one of 4 quotes for when the user gets the question right, or one of 4 when the user gets it wrong, using the window.alert method.
Now I'm very new to Javascript, so its probably a small mistake. I have written the functions necessary to pick a random quote and display it, but when I call the function from the function "buttonPressed", no window alert comes up. What am I doing wrong?
My Code So Far:
- PHP: Select all
<script type="text/javascript">
var x,y;
window.alert( "Welcome to the 'Learning How To Multiply' website! \n\nThis website aims to help children who have mastered their times tables, to take the next step: multiplying numbers outside of the context of times tables. \n\nYour child will be presented with a series of multiplication questions, testing their ability to understand the concept of simple multiplication. \n\nWe hope that with persistance and determination, this exercise will improve your child's arithmatic skills and confidence. \n\nGood Luck!" );
window.onload=genQuestion;
function genQuestion()
{
for ( var i = 1; i <= 1; i++ )
{
x = Math.floor( 1 + Math.random() * 12 );
y = Math.floor( 1 + Math.random() * 12 );
document.multiForm.question1.value= x + " times " + y + "?";
if ( i % 11 == 0 && i != 20 ){}
//document.write();
}
}
function get_random()
{
var ranNum= Math.floor(Math.random()*4);
return ranNum;
}
var whichcorrectQuote=get_random();
var correctQuote=new Array(4)
{
correctQuote[0]=("Very good!");
correctQuote[1]=("Excellent!");
correctQuote[2]=("Correct! Keep up the good work.");
correctQuote[3]=("Correct, Well done!");
}
var whichwrongQuote=get_random();
var wrongQuote=new Array(4)
{
wrongQuote[0]="No. please try again.";
wrongQuote[1]="Try once more.";
wrongQuote[2]="Incorrect. Don't give up!";
wrongQuote[3]="No, Keep trying.";
}
function buttonPressed()
{
din = document.getElementById("inputVal");
if (parseInt(din.value) == (x*y))
{
window.alert (whichcorrectQuote());
document.multiForm.reset();
genQuestion();
document.multiForm.inputVal.focus();
}
else
{
window.alert (whichwrongQuote());
document.multiForm.inputVal.value="";
document.multiForm.inputVal.focus();
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<h1>Learn to Multiply!</h1>
<p>Hello there! Are you ready to multiply? Great! Lets go!</p>
<!--This uses a table format. First the HTML part. Each time <tr> is written, a new row is created. -->
<form name="multiForm" id="multiForm" action="">
<table border = "1">
<tr>
<td>What is: </td>
<td><input name="question1" type="text" value=""></td>
</tr>
<tr>
<td>The answer is: </td>
<td><input id="inputVal" name="inputVal" type="text"></td>
</tr>
<tr>
<td><input name="Check my Answer!" type="button" value="Check my Answer!" onclick="buttonPressed()"></td>
</tr>
</table>
</form>
Thanks for any help.
