View Single Post
  #1 (permalink)  
Old Dec 7th, 2007, 16:31
Z101 Z101 is offline
New Member
Join Date: Dec 2007
Location: UK
Age: 19
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Problem with displaying random quotes from a function

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 
1<= 1i++ )
        {
            
Math.floorMath.random() * 12 );
            
Math.floorMath.random() * 12 );

            
document.multiForm.question1.value" times " "?";
            if ( 
11 == && != 20 ){}
            
//document.write();
            
        
}
    }

function 
get_random()
{
var 
ranNumMath.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.

Last edited by Lchad; Dec 8th, 2007 at 14:16. Reason: took out link... not allowed.
Reply With Quote