Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts
    As a start, use post #20 and after from Non-repeat random number generator?

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

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I looked at it but it seemed to complex for what I need, please help.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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?

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    emination (08-19-2011)

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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!

  • Users who have thanked jmrker for this post:

    emination (08-19-2011)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Ask and ye shall receive. Well, if the mood is right. <grin/>

  • #9
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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!

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #12
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.


  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #14
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •