Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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.

• What have you attempted thus far?

• Originally Posted by jmrker
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

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

• Originally Posted by Old Pedant
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.

• 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

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

• Originally Posted by Old Pedant
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>
<title></title>
<body>
<center>
<h1 size='20'>Quiz</h1>
<font size='2'><p>Please select a difficulty level</p></font>
<form name="form">
</textarea>
</textarea>
<p><input type='button' value='Begin!' onclick='startprog()'/>
</center>
<script language="JavaScript" type="text/javascript">
function startprog()
{
if(document.form.g1.checked)
{
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")
{
if(ans = "A" || "a")
{
}
else
{
}
}
else if(questionseasy[q] == "This is question 2")
{
}
}
}
else if(document.form.g1.checked)
{
}
else if(document.form.g3.checked)
{
}

else
{
//they DIDNT CHECK ANYTHING GET OUT.
return false;
}
}
</script>

</body>
</html>```
Note: I put values for strings.

• 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( )

• ## Users who have thanked Old Pedant for this post:

dakoslug (05-17-2012)

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

• ## Users who have thanked Philip M for this post:

dakoslug (05-17-2012)

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

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

• Originally Posted by dakoslug
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. ```

• Originally Posted by Old Pedant
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.

• Originally Posted by dakoslug
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.

•
Page 1 of 2 12 Last

1. ###### JavaScript - Random Question Quiz - AllWebDevHelp.com
02-27-2014, 02:44 PM

#### Posting Permissions

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