Web Analytics Made Easy -
StatCounter Random Question Generator - CodingForum

Announcement

Collapse
No announcement yet.

Random Question Generator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    As a start, use post #20 and after from http://www.codingforum.net/showthrea...=235040&page=2

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

    Comment


    • #3
      I looked at it but it seemed to complex for what I need, please help.

      Comment


      • #4
        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?
        Be yourself. No one else is as qualified.

        Comment


        • #5
          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

          Comment


          • #6
            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.
            Be yourself. No one else is as qualified.

            Comment


            • #7
              Originally posted by emination View Post
              I looked at it but it seemed to complex for what I need, please help.
              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!

              Comment


              • #8
                Ask and ye shall receive. Well, if the mood is right. <grin/>
                Be yourself. No one else is as qualified.

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    Originally posted by Old Pedant View Post
                    Ask and ye shall receive. Well, if the mood is right. <grin/>
                    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!

                    Comment


                    • #11
                      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.
                      Be yourself. No one else is as qualified.

                      Comment


                      • #12
                        __________________
                        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.

                        Comment


                        • #13
                          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.
                          Be yourself. No one else is as qualified.

                          Comment


                          • #14
                            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.

                            Comment


                            • #15




                              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!]
                              Be yourself. No one else is as qualified.

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎