View Full Version : Multiple Choice program problem

07-30-2010, 01:58 PM
Hello, I am currently making a multiple choice program in JS using 3 arrays, one to call the question, one to store the user answer and one to store the real answer. The answers will then be compared to find the score.

I have done most of it, but am still having one problem, I am using prompts and when I call the question, only the text in the if statement "qArray[counter]" is actually showing on the prompt and not the actual question from the array and I wondered if anyone could help?


<head><B>Multiple Choice Quiz</B>
<script language = 'JavaScript'>

document.write('<br><br>This program allows multiple choice questions to be asked - once finished a calculation will total your score.<br>');

qArray = new Array();
qArray = ['What is the capital of England? London/Paris/Cardiff/Berlin', 'What is the capital of France? London/Paris/Cardiff/Berlin', 'What is the capital of Wales? London/Paris/Cardiff/Berlin', 'What is the capital of Germany? London/Paris/Cardiff/Berlin'];

aArray = new Array();
aArray = ['London', 'Paris', 'Cardiff', 'Berlin']; //array of the answers
counter = 0;
NumberofQuestions = 4;

score = 0;

while (counter < NumberofQuestions){
if (counter == 0){
if (counter == 1){
if (counter == 2){
if (counter == 3){
studentAnswers = new Array();
studentAnswers = [question1, question2, question3, question4]; //an array with the answers the student has inputted
counter = 0;

while (counter < NumberofQuestions){
if (studentAnswers[counter] == aArray[counter]){

document.write ('You have finished the quiz, your total marks will be calculated.'); //inform student that they have finished

document.write('You scored: ',score,' /4'); //prints the score to screen.


Philip M
07-30-2010, 02:38 PM
Remove the single quotes around question1=prompt('qArray[counter]','');
i.e. question1=prompt(qArray[counter],'');
With quotes it is a literal.

I expect you realise that as with any Javascript quiz the user can see the answers with View Source. At a minimum you should place
aArray = ['London', 'Paris', 'Cardiff', 'Berlin']; //array of the answers
in an external .js file.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

I fear that you are really going about this the wrong way. First of all, in Internet Explorer 7.0 when you try to use the javascript "prompt" command you'll get a security warning at the top of the window stating:
This website is using a scripted window to ask you for information. If you trust this website, click here to allow scripted windows...

If the possible answers are predetermined (restricted to certain choices) you should use radio buttons:-

What is the capital of England?
London <input type = "radio" name = "rad1">
Paris <input type = "radio" name = "rad1">
Cardiff <input type = "radio" name = "rad1">
Berlin <input type = "radio" name = "rad1">

There is surely no point in recording the student's wrong answers. They will not survive the browser session.

Here is a script which may serve you better:-

<form name = "myform">
Q1 What colour is the sky?<br>
Red <input type = "radio" name = "Q1" value = "0Red">
Blue <input type = "radio" name = "Q1" value = "1Blue">
Green <input type = "radio" name = "Q1" value = "0Green">
Yellow <input type = "radio" name = "Q1" value = "0Yellow">
Q2 Which fruit is normally the smallest?<br>
Apple <input type = "radio" name = "Q2" value = "0Apple">
Banana <input type = "radio" name = "Q2" value = "0Banana">
Pear <input type = "radio" name = "Q2" value = "0Pear">
Grape <input type = "radio" name = "Q2" value = "1Grape">
Q3 Which animal has the longest ears?<br>
Dog <input type = "radio" name = "Q3" value = "0Dog">
Cat <input type = "radio" name = "Q3" value = "0Cat">
Rabbit <input type = "radio" name = "Q3" value = "1Rabbit">
Hamster <input type = "radio" name = "Q3" value = "0Hamster">
<input type = "button" name= "but1" value = "Check The Answers" onclick = "chkrads('myform')">

<script type = "text/javascript">

function chkrads(frmName) {
var totScore = 0;
for (var j = 1; j<=3; j++) {
var rbGroupName = "Q"+j;
var chosen = "None";
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {

if (radios[i].checked) {
rval = radios[i].value.substring(0,1);
chosen = rval;
strVal = radios[i].value.substring(1);
if (chosen == "None") {
alert("No selection chosen for " + rbGroupName + ". Please answer this question.");
return false;
else {
alert("Your selected answer for " + rbGroupName + " was " + strVal + ". Your answer scored " + chosen + " points ");
totScore = totScore + parseInt(chosen);

alert ("Your total score for this quiz was " + totScore + " points ");


The quiz may be obfuscted/scrambled by using http://www.voormedia.com/en/tools/html-obfuscate-scrambler.php

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

08-02-2010, 04:29 PM
Thanks, it got it working.
I'm also wondering how to make it show as percentage instead of 1/4 etc. It's only going to have 4 questions, so will be 0, 25, 50, 75 or 100%, but not sure how I would get that working?


Philip M
08-02-2010, 05:09 PM
I'm also wondering how to make it show as percentage instead of 1/4 etc. It's only going to have 4 questions, so will be 0, 25, 50, 75 or 100%, but not sure how I would get that working?

Oh, come now! That is covered in primary school.:eek:

var score = 3
var percent = score/4 * 100 + "%";
alert (percent);