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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Random question Quiz

    Hi, im trying to make a quiz. Now, what I have realized is that I can make a random number coincide with a question. But then I'd have to do a huge amount of if statements. Is there a MUCH simpler way of doing this? And also the questions CAN NOT repeat.
    Please try and keep it simple, as I'm trying to learn.
    There's 10 questions by the way.

    Thanks in advance!
    Last edited by dakoslug; 05-17-2012 at 01:37 AM.

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

    Question

    What have you attempted thus far?

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Netscape

    Quote Originally Posted by jmrker View Post
    What have you attempted thus far?
    This extremely long code.
    I won't paste everything here but I'll give u an example on what i've done so far.

    var ques1 = Math.floor(Math.random() * 10)
    if(ques1 == 1)
    {
    var ans1 = prompt("Blah blah question")
    if(ques1 = a) //a is the correct answer
    {
    var ques2 = Math.floor(Math.random()* 9)
    if(ques2 == 1)
    {
    //next question
    }
    }
    }

    Goes on like this forever
    Last edited by dakoslug; 05-17-2012 at 12:38 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Okay...I'll give you something to get you started:
    Code:
    var questions = [
       "This is question 1",
       "This is question 2",
       ...
       "This is question 10"
       ];
    
    questions.sort( function() { return Math.random() - 0.5; } );
    
    // now the questions will be in random order so you can just do
    for ( var q = 0; q < questions.length; ++q )
    {
        var currentQuestion = questions[q];
        ... what do you do with it? ...
    }
    ...
    BUT...

    But now how do you associate the right answers with the right questions?

    And and and and...

    TONS more details.
    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.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay...I'll give you something to get you started:
    Code:
    var questions = [
       "This is question 1",
       "This is question 2",
       ...
       "This is question 10"
       ];
    
    questions.sort( function() { return Math.random() - 0.5; } );
    
    // now the questions will be in random order so you can just do
    for ( var q = 0; q < questions.length; ++q )
    {
        var currentQuestion = questions[q];
        ... what do you do with it? ...
    }
    ...
    BUT...

    But now how do you associate the right answers with the right questions?

    And and and and...

    TONS more details.

    Ahh :c
    Is what you are showing me pseudo code or does everything work as it should
    Also how could I alert the question? Edit: NVM figured out how to alert.

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Also how does this work.

    questions.sort( function() { return Math.random() - 0.5; } );


    And I'm pretty sure this doesn't stop the questions from being back to back correct? This is what i want sorry :c
    Last edited by dakoslug; 05-17-2012 at 01:32 AM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    That shuffles the order of the questions in the array (it's not a perfect shuffle, but it's good enough for something like this).

    That is, if you had questions 1 to 10 in the array, after the shuffle they might be in the order
    7,1,3,8,9,2,5,10,4,6

    So if you then pull them *out* of the array in array order (0 through 9), you will get all the questions, one time each, in random order.
    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.

  • #8
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    That shuffles the order of the questions in the array (it's not a perfect shuffle, but it's good enough for something like this).

    That is, if you had questions 1 to 10 in the array, after the shuffle they might be in the order
    7,1,3,8,9,2,5,10,4,6

    So if you then pull them *out* of the array in array order (0 through 9), you will get all the questions, one time each, in random order.
    Ah it does work the way I want it to!
    Thanks!

    Here's what i have so far.

    Please tell me if I'm on the right track.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <center>
    <h1 size='20'>Quiz</h1>
    <font size='2'><p>Please select a difficulty level</p></font>
    <form name="form">
    <input type="radio" name="diff" id='g1'value="easy">Easy
    <input type="radio" name="diff" id='g2'value="hard">Medium
    <input type="radio" name="diff" id='g3'value="hard">Hard
    <p><textarea name="comments" cols="40" rows="5" id='comments'>
    </textarea>
    <p><textarea name="answers" cols="40" rows="3" id='answers'>
    </textarea>
    <p><input type='button' value='Begin!' onclick='startprog()'/>
    </center>
    <script language="JavaScript" type="text/javascript">
    function startprog()
    {
    if(document.form.g1.checked) 
     {
      document.getElementById('comments').innerHTML = "Easy Difficulty";
      alert("You selected Easy difficulty.")
    	var questionseasy = [
       "This is question 1",
       "This is question 2",
       "This is question 3",
    	 "This is question 4",
    	 "This is question 5",
    	 "This is question 6",
    	 "This is question 7",
    	 "This is question 8",
    	 "This is question 9",
    	 "This is question 10"
       ];
    questionseasy.sort(function() { return Math.random() - 0.5; } );
    for (var q = 0; q < questionseasy.length; ++q )
    {
        var currentques = questionseasy[q];
    		if(questionseasy[q] == "This is question 1")
    		{
    		document.getElementById('comments').innerHTML = "Question 1";
    		document.getElementById('answers').innerHTML = "Answer";
    		var ans = prompt("Answer?","")
    		if(ans = "A" || "a")
    		 {
    		 document.getElementById('comments').innerHTML = "Correct!";
    		 document.getElementById('answers').innerHTML = "";
    		 }
    		else
    		 {
    		 document.getElementById('comments').innerHTML = "Incorrect";
    		 document.getElementById('answers').innerHTML = "";
    		 }
    		}
    		else if(questionseasy[q] == "This is question 2")
    		{
    		alert("Question 2 has appeared!")
    		}
    } 
     }
    else if(document.form.g1.checked) 
     {
      document.getElementById('comments').innerHTML = "Medium Difficulty";
      alert("You selected medium difficulty.")
     } 
    else if(document.form.g3.checked)
     {
     document.getElementById('comments').innerHTML = "Hard Difficulty";
     alert("You selected hard difficulty.")
     } 
    
    else
     {
     //they DIDNT CHECK ANYTHING GET OUT.
     alert("No difficulty level selected")
     return false;
     } 
    } 
    </script>
    
    </body>
    </html>
    Note: I put values for strings.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    That will work, but it's about 10 times more code than you need if you figure out the right way to do it.

    And it really seems awkward, to me, to combine innerHTML (to put questions in place) and then using ugly old prompt to get the user's answers.

    I assume this is homework, right?

    Here's another hint for you:

    If you make each "question" *actually* be an array that has both the question and the answer, then when you shuffle the questions, you will be shuffling the answers right along with the questions.

    Code:
    var questions = [
        [ "How many quarts in a gallon?", "4" ],
        [ "What is the capital of Massachusetts?", "Boston" ],
        ...
    ];
    And try hard to avoid using prompt( )
    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.

  • Users who have thanked Old Pedant for this post:

    dakoslug (05-17-2012)

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Another way using a one-dimensional array is to separate question and answer with a delimiter such as ~ and then split the result:-

    Code:
    var questions = []
    questions[0] = "How may quarts in a gallon?~4";
    questions[1] = "What is the capital of Massachusetts?~Boston";
    questions[2] = "Who is the President of the USA?~Barack Obama";
    for (var i =0; i<questions.length; i++) {
    var t = questions[i].split("~");
    var q = t[0];
    var a = t[1];
    alert (q + "  " + a);
    }
    felgall has pointed out that

    questions.sort( function() { return Math.random() - 0.5; } );

    is strongly biased towards the original order. You can improve the randomness of the sort by changing 0.5 to 0.75.
    Last edited by Philip M; 05-17-2012 at 10:05 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    dakoslug (05-17-2012)

  • #11
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well actually prompt is what the teacher actually taught me.
    This is a culminating assignment and I have to show my understanding how prompt works >.< .

    I'll try splitting the values in arrays but personally I think I'd rather keep it simple with the if statements. When I get more advanced I must learn such methods.

    Thanks guys!

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    It is much more COMPLICATED with the IF statements!!! BY a factor of TEN if you indeed have 10 questions. Or by 30 if you have 3 sets of 10 questions.

    And, as Felgall would say, if your teach is teaching you to use prompt() then he/she is teaching you 20th century JavaScript instead of being in the 21st century.
    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.

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by dakoslug View Post
    This is a culminating assignment and I have to show my understanding how prompt works
    Well prompt is a debugging tool - in some browsers it provides a checkbox for disabling JavaScript on the current page and in others it provides a checkbox for disabling all future dialog boxes from the current script. Therefore any legitimate use of prompt would have a script that doesn't work where you insert a prompt just prior to the spot you want to test so that you can easily replace the current value the script has at that point with a different value. If the script gets into an infinite loop you can then use the checkbox to exit from the script once you have checked enough times through to figure out what is causing it to get stuck.

    So to illustrate an understanding of how to use prompt you'd need a script that doesn't work that is possibly getting stuck in an infinite loop so that you can stick the debugging statement into the code to help you fix it. Of course simply learning how to use the built in JavaScript debugger is a better option than using ad hoc debugging tools such as alert confirm and prompt but using the dialogs - particularly alert - is generally quicker in most cases with short scripts.

    The following is the way the alert dialog looks in one browser.



    and here's what the checkbox looks like in an alert in another browser:



    Note that I use alert as my example images because alert is generally far more useful than confirm or prompt (since you generally want to see what values veriables have during debugging without needing to change them). All three have the exact same checkbox in them though to allow the person running the script to either turn off JavaScript or disable further dialogs.

    So anyone visiting a live site that uses prompt can select "disable JavaScript" or "don't create additional dialogs" if they want to and the alert/confirm/prompt is in fact suggesting that they do so. At that point you may as well not have any of the JavaScript after that statement as once your visitor selects to turn off JavaScript no further scripts will run in that entire web page.


    Just to make it absolutely clear that prompt is intended as a debugging tool only, here's what one looks like in one browser just so you can see for yourself that the checkbox for turning off JavaScript is the same as in the alert.
    Last edited by felgall; 05-18-2012 at 11:10 AM. Reason: Add prompt dialog.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    New Coder
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    It is much more COMPLICATED with the IF statements!!! BY a factor of TEN if you indeed have 10 questions. Or by 30 if you have 3 sets of 10 questions.

    And, as Felgall would say, if your teach is teaching you to use prompt() then he/she is teaching you 20th century JavaScript instead of being in the 21st century.

    i'm using the arrays, just i rather use prompt for the input so i can get a good mark.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by dakoslug View Post
    i'm using the arrays, just i rather use prompt for the input so i can get a good mark.
    This is a serious dilemma for a student. If the teacher requires the student to produce antique/obsolete Javascript (reflecting his own lack of understanding of modern Javascript), then the student must necessarily comply, even though as a visitor to this site he has been made aware of the proper methods.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  
    Page 1 of 2 12 LastLast

    LinkBacks (?)


    Posting Permissions

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