Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2011, 01:45 AM   PM User | #1
emination
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
emination is an unknown quantity at this point
Internet Explorer Random Question Generator

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

Code:
<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
emination is offline   Reply With Quote
Old 08-19-2011, 01:52 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
As a start, use post #20 and after from http://www.codingforums.com/showthre...=235040&page=2

It has a lot more than you have provided thus far.
jmrker is offline   Reply With Quote
Old 08-19-2011, 02:03 AM   PM User | #3
emination
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
emination is an unknown quantity at this point
I looked at it but it seemed to complex for what I need, please help.
emination is offline   Reply With Quote
Old 08-19-2011, 02:15 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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?
Old Pedant is offline   Reply With Quote
Old 08-19-2011, 02:24 AM   PM User | #5
emination
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
emination is an unknown quantity at this point
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
emination is offline   Reply With Quote
Old 08-19-2011, 03:43 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
<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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
emination (08-19-2011)
Old 08-19-2011, 04:45 AM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Arrow

Quote:
Originally Posted by emination View Post
I looked at it but it seemed to complex for what I need, please help.
Quote:
Originally Posted by Old Pedant View Post
Code:
<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!
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
emination (08-19-2011)
Old 08-19-2011, 04:52 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ask and ye shall receive. Well, if the mood is right. <grin/>
Old Pedant is offline   Reply With Quote
Old 08-20-2011, 12:29 AM   PM User | #9
emination
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
emination is an unknown quantity at this point
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.
emination is offline   Reply With Quote
Old 08-20-2011, 02:29 AM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Thumbs up

Quote:
Originally Posted by Old Pedant View Post
Ask and ye shall receive. Well, if the mood is right. <grin/>
Quote:
Originally Posted by emination View Post
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?
Amazing!
jmrker is offline   Reply With Quote
Old 08-20-2011, 08:07 PM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-20-2011, 08:58 PM   PM User | #12
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Thumbs up

__________________
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.

jmrker is offline   Reply With Quote
Old 08-20-2011, 09:02 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-20-2011, 10:05 PM   PM User | #14
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Smile

Quote:
Originally Posted by Old Pedant View Post
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.
jmrker is offline   Reply With Quote
Old 08-20-2011, 10:13 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
http://www.straightdope.com/columns/...r-all-good-men

http://writingacts.wordpress.com/200...-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!]
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
generator, questions, random, simple

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:21 AM.


Advertisement
Log in to turn off these ads.