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
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?
<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.
I looked at it but it seemed to complex for what I need, please help.
Quote:
Originally Posted by Old Pedant
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!
"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.
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.
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.
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.