Problem with displaying random quotes from a function

This is a discussion on "Problem with displaying random quotes from a function" within the JavaScript Forum section. This forum, and the thread "Problem with displaying random quotes from a function are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 7th, 2007, 16:31
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

  #2 (permalink)  
Old Dec 7th, 2007, 17:46
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: Problem with displaying random quotes from a function

Your problem is here:

Code: Select all
...
window.alert (whichcorrectQuote());
...
window.alert (whichwrongQuote());
...
They are not functions, but variables so should be :

Code: Select all
...
window.alert (whichcorrectQuote);
...
window.alert (whichwrongQuote);
...
Finally you need to set the variable to the array matching the random name created like so:

Code: Select all
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 whichcorrectQuote=correctQuote[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.";
}
var whichwrongQuote=wrongQuote[get_random()];
See how you set the variable to the array name with the getRandom() function returning the array key as the number it made up.

Blake.
Reply With Quote
  #3 (permalink)  
Old Dec 7th, 2007, 17:59
New Member
Join Date: Dec 2007
Location: UK
Age: 19
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem with displaying random quotes from a function

Hey thanks, that fixes one problem, the quotes are now coming up when the button is pressed!

Although, it is only selecting one of the quotes each time the button is pressed. IE: "Correct well done" for the right answer, "No, please try again" for the wrong answer. It doesn't seem to be randomly selecting any of the other quotes

Is there any way to fix this?
Reply With Quote
  #4 (permalink)  
Old Dec 7th, 2007, 22:34
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: Problem with displaying random quotes from a function

Add the lines in bold to the following statement:

Code: Select all
if (parseInt(din.value) == (x*y))
            {
            whichcorrectQuote=correctQuote[get_random()];
            window.alert (whichcorrectQuote);
            document.multiForm.reset();
            genQuestion();
            document.multiForm.inputVal.focus();
            }
            
        else
            {
            whichwrongQuote=wrongQuote[get_random()];
            window.alert (whichwrongQuote);
            document.multiForm.inputVal.value="";
            document.multiForm.inputVal.focus();
            }

This will reset the variable each time by choosing a random number every time the script is run instead of just once.

You can also delete the bold lines from above the statement. They are just extra code. e.g.

Code: Select all
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 whichcorrectQuote=correctQuote[get_random()]; // << Take out this line
Blake.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem: onClick Function mihirc Web Page Design 16 Jul 28th, 2007 11:20
Problem with random margin in IE? FF OK bobfish Web Page Design 2 Jul 4th, 2007 08:34
Function parameters that include quotes loorp JavaScript Forum 7 Sep 17th, 2006 04:18
Function for Displaying Month chlad Classic ASP 1 Sep 2nd, 2006 16:52
XSL Displaying Problem ivan Other Programming Languages 0 Nov 23rd, 2005 01:20


All times are GMT. The time now is 22:29.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43