PDA

View Full Version : [help] Javascript Random Generator



mamamiaw
08-03-2008, 04:41 AM
Hi all, Sorry to bother u all, I think i previously post about the quiz script, and i read and research abit here and there,

anyway i manage to write some but found some difficulties,especially about random generator..

I have this several 'set' of question and answer, and i like to view'em random
(e.g: refresh---> What is 1+1=?...with option n aswers, refresh---> What is 1+1=?...with option n aswers, refresh---> What is

1+1=?...with option n aswers, ...etc)



questionarray[0] ='What is 1+1=?.'
answer[0] ='1'
Adistract[0] ='2'
Bdistract[0] ='22'
Cdistract[0] ='11'
Ddistract[0] ='0'
questionarray[1] ='what is the color of the sky?.'
answer[1] ='C'
Adistract[1] ='red'
Bdistract[1] ='yellow'
Cdistract[1] ='blue'
Ddistract[1] ='purple'
questionarray[2] ='cow is =?.'
answer[0] ='D'
Adistract[0] ='Tank'
Bdistract[0] ='President'
Cdistract[0] ='Weapon'
Ddistract[0] ='Animal'
questionarray[3] ='What is 2+2=?.'
answer[0] ='A'
Adistract[0] ='4'
Bdistract[0] ='22'
Cdistract[0] ='11'
Ddistract[0] ='0' .....etc


and also if any of master n gurus here have better suggestion, both on the question array and get (viewing method on browser),

i'll be very thankful.
Anyway, thanks in advance.

Best Regards

~M~

Jesdisciple
08-04-2008, 05:05 AM
This sounds like it would be better suited to PHP...

Also, object-orientation (http://en.wikipedia.org/wiki/Object-oriented_programming) would be much more appropriate than your current approach. And I think you're confusing yourself in that anyway...

Here's a randomizer, where answers is the array of such. EDIT: Note that this is intended to go inside a for-loop.
var index = Math.floor(Math.random() * answers.length);
var current = answers.splice(index, 1);//Remove the element so it won't be used again.
//Build the question from 'current'.It's a good idea to copy the array (var copy = answers.concat();) and reassign it when you're done building the question.

Ask questions. :)

mamamiaw
08-04-2008, 07:08 PM
Hi thanks for your kind reply ^_^

Heres the complete questions:

for this quiz
There will be no ask the audience or phone a friend
This game will be a Pure javascript , played offline.
The contestant (the student) will only see the screen, they're not allowed to have keyboard or mouse.

Anyway, here i upload the thing that i create...to explain the situation.

DOWNLOAD HERE (http://www.lajusaja.com/quizhelp.zip)


As u can see...on the javascript file (millquest.js) , i only manage to create a set....
which are:
stemarray[0] ='Monas adalah singkatan dari:'
answer[0] ='B'
adistract[0] ='Monumen Naskah'
bdistract[0] ='Monumen Nasional'
cdistract[0] ='Museum Nasional'
ddistract[0] ='Museum Naskah'

...all till number 7

But what i want to create are:

for question number 1-3 (which the contestant will win 100 point to Hadiah A), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 4-5 (which the contestant will win 100 point to Hadiah B), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 6 (which the contestant will win 100 point to Hadiah C), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 7 (which the contestant will win 100 point to Hadiah D), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 8 (which the contestant will win 100 point to Hadiah E), they consist of 75 questions that will be randomly displayed each time the quiz begin


So sorry but i don't understand PHP at all...(not a real programmer myself)

Thanks for your answer, if u don't mind, can u explain a bit about it...i mean i've read about the math.random, but not really understand of the usage and application to my quiz ^_^

And also...pardon my bad english...^_^

Thanks alot...

Best regards

Jesdisciple
08-04-2008, 07:56 PM
EDIT: And forget the PHP comment; PHP is a server script which you indicated you can't use in the other thread.

Math.random returns a pseudo-random number greater than or equal to 0 and less than 1 (0 <= X < 1). Multiplying that number by answers.length and passing the product (multiplication result) to Math.floor gives an integer (whole number) within the range [0 <= X < answers.length]. That integer can then be used as a random index to the answers array.

The Array.splice (link) (http://w3schools.com/jsref/jsref_splice.asp) method then removes 1 element from the array at the specified index and returns it to the current variable.

And you're definitely doing better with my language than I am with yours (because I don't have a second language to speak of). But what is your language? I need to translate some of the terms involved with the game to better understand it.

mamamiaw
08-05-2008, 04:02 AM
Thanks for the explanation....^_^

My first language is indonesia...i'm an indonesian, asian to be general
Which one that u need to translate?.. i can translate it to you ^_^

Thanks alot

Jesdisciple
08-06-2008, 03:31 AM
I guess "Hadiah" means "Section"? What are the related details?