...

View Full Version : Random Question Generator



emination
08-19-2011, 01:45 AM
Hey I'm somewhat new to the javascript world because 95% of my website is HTML so, if you will, I need help writing the javascript for a Random Question Generator...basicly all I have is


<script type="text/javascript">
var Q = new Array()
Q[1] = "What is your favorite color?";
Q[2] = "What is your favorite animal?";
Q[3] = "What is your favorite place?";
</script>

In the real one, there will be about 20 questions and I will need it to choose a quetion randomly out of the 20. As I said i am a beginer so as simple as possible would be great. Thank You for your help in advance:)

jmrker
08-19-2011, 01:52 AM
As a start, use post #20 and after from http://www.codingforums.com/showthread.php?t=235040&page=2

It has a lot more than you have provided thus far.

emination
08-19-2011, 02:03 AM
I looked at it but it seemed to complex for what I need, please help.

Old Pedant
08-19-2011, 02:15 AM
Do you only need one question out of the set? Or do you need to be able to display N questions (you pick N)? If you need N, then do you want them all to show up when the page first opens or should they appear one at a time?

emination
08-19-2011, 02:24 AM
I need the questions to show up one at a time but at random, do you get what I'm trying to make? Thanks for the help :)

Old Pedant
08-19-2011, 03:43 AM
<script type="text/javascript">
// simpler way to make the array:
var questions = [
"What is your favorite color?",
"What is your favorite animal?".
"What is your favorite place?"
];

var question = questions[ Math.floor( Math.random() * questions.length ) ];
document.write( '<div id="TheQuestion">' + question + '</div>' );
</script>

The document.write and the div are just an example of usage.

You can put as many strings in the array as you want. Put a comma after all except the last one.

jmrker
08-19-2011, 04:45 AM
I looked at it but it seemed to complex for what I need, please help.




<script type="text/javascript">
// simpler way to make the array:
var questions = [
"What is your favorite color?",
"What is your favorite animal?".
"What is your favorite place?"
];

var question = questions[ Math.floor( Math.random() * questions.length ) ];
document.write( '<div id="TheQuestion">' + question + '</div>' );
</script>

The document.write and the div are just an example of usage.

You can put as many strings in the array as you want. Put a comma after all except the last one.

Well, that is less complex ... Doesn't do much more that what the OP requested ... But is is less complex! :thumbsup:

Old Pedant
08-19-2011, 04:52 AM
Ask and ye shall receive. Well, if the mood is right. <grin/>

emination
08-20-2011, 12:29 AM
Thank you sooo much:) the code worked just like I needed it to. Thank you again Old Pedant for making it as simple as possible:) Keep up the great work.

jmrker
08-20-2011, 02:29 AM
Ask and ye shall receive. Well, if the mood is right. <grin/>


Thank you sooo much:) the code worked just like I needed it to. Thank you again Old Pedant for making it as simple as possible:) Keep up the great work.

Well, "Old Pedant", I'm officially amazed. How on earth did you figure out that was what was needed from the information provided? :thumbsup:
Amazing!:D

Old Pedant
08-20-2011, 08:07 PM
Educated guess. Or maybe experienced guess.

"one at a time" *probably* meant "one per page". So I took a stab. He also said he was a beginner, so I assumed/hoped he really was looking for something dead simple.

But of course I could easily have been wrong. Seemed worth a stab.

jmrker
08-20-2011, 08:58 PM
__________________
Now is the time for all good men to come to.
There's a longer version, but it's much better to stop where this one does.

I like the sexier version:

__________________
Now is the time for all good men to come.

:eek: :thumbsup: :D

Old Pedant
08-20-2011, 09:02 PM
Well, mine's a political commentary, of sorts. Yours is just fun. <snicker/>

Seriously, the original ends with "...to the aid of the party." And I really dislike that. There's no "party" that I feel like aiding right now. They all need to get their heads out of the sand.

jmrker
08-20-2011, 10:05 PM
Well, mine's a political commentary, of sorts. Yours is just fun. <snicker/>

Seriously, the original ends with "...to the aid of the party." And I really dislike that. There's no "party" that I feel like aiding right now. They all need to get their heads out of the sand.

I thought it was "... to the aid of their country." which I have always felt was a bit more patriotic. :rolleyes:

Old Pedant
08-20-2011, 10:13 PM
http://www.straightdope.com/columns/read/259/who-originated-now-is-the-time-for-all-good-men

http://writingacts.wordpress.com/2007/11/05/now-is-the-time-for-all-good-men-to-come-to-the-aid-of-the-party/

I actually *learned* the phrase in typing class. Yeah, I was one of two boys in a class of around 30. Took the class during summer school between my freshman and sophomore years in h.s. Never regretted it. Especially many years later when I first sat down in front of a keypunch (and *that* dates me, doesn't it?).

I can still type around 45 WPM, maybe faster. And all touch type, of course.

[Good grief! That was 52 years ago!]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum