...

View Full Version : Random question Quiz



dakoslug
05-17-2012, 01:05 AM
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!

jmrker
05-17-2012, 01:19 AM
What have you attempted thus far?

dakoslug
05-17-2012, 01:25 AM
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

Old Pedant
05-17-2012, 02:16 AM
Okay...I'll give you something to get you started:


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.

dakoslug
05-17-2012, 02:23 AM
Okay...I'll give you something to get you started:


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.

dakoslug
05-17-2012, 02:29 AM
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

Old Pedant
05-17-2012, 02:43 AM
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.

dakoslug
05-17-2012, 02:53 AM
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.


<!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.

Old Pedant
05-17-2012, 04:00 AM
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.



var questions = [
[ "How many quarts in a gallon?", "4" ],
[ "What is the capital of Massachusetts?", "Boston" ],
...
];


And try hard to avoid using prompt( )

Philip M
05-17-2012, 10:59 AM
Another way using a one-dimensional array is to separate question and answer with a delimiter such as ~ and then split the result:-


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.

dakoslug
05-17-2012, 10:22 PM
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!

Old Pedant
05-17-2012, 11:44 PM
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.

felgall
05-18-2012, 11:53 AM
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.

http://javascriptexample.net/inc/alert.gif

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

http://javascriptexample.net/inc/alert2.gif

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.
http://javascriptexample.net/inc/prompt.gif

dakoslug
05-18-2012, 04:26 PM
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.

Philip M
05-18-2012, 06:12 PM
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.

dakoslug
05-18-2012, 09:32 PM
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.

I don't really pay attention to his lessons actually. All that he has taught I've learned through ways like this thread. But the rubric for this 30% of final mark assignment is that the student must show his/her understanding of the methods the teacher has taught. Half of the things that I put in my assignment, such as arrays and document.write and other methods, he didn't teach. Things like prompts, ifs and for loops, he did. My teacher claims hes more native to Python and this is his first year of teaching JavaScript.

I'll continue to practice more modern terms of coding :P by taking up your suggestions outside of class.

felgall
05-18-2012, 10:10 PM
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.

There's no reason why the student shouldn't learn why to not use that code after completing that class though - even if they don't want to go as far as telling the teacher why the code they are being taught is obsolete. At least then after finishing the class they can then start doing things properly.

Would people really continue using prompt to try to collect data if they knew that a percentage of their visitors are also being asked if they want to "Stop executing scripts on this page" and an even larger number are being asked if they want to "Prevent this page from producing additional dialogues". The problem is that the place where the class is being taught probably is also using an antiquated browser that doesn't display either of those messages the way most modern browsers do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum