Results 1 to 6 of 6

Thread: [help] Javascript Random Generator

  1. #1
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [help] Javascript Random Generator

    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)

    Code:
    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~

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    This sounds like it would be better suited to PHP...

    Also, object-orientation 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.
    Code:
    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.
    Last edited by Jesdisciple; 08-04-2008 at 05:18 AM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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


    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
    Last edited by mamamiaw; 08-04-2008 at 07:22 PM.

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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) 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.
    Last edited by Jesdisciple; 08-04-2008 at 08:09 PM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. The Following User Says Thank You to Jesdisciple For This Useful Post:

    mamamiaw (08-05-2008)

  6. #5
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I guess "Hadiah" means "Section"? What are the related details?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

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