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.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple Choice program problem

    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?

    Thanks


    <html>
    <head><B>Multiple Choice Quiz</B>
    </head>
    <body>
    <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){
    question1=prompt('qArray[counter]','');
    }
    if (counter == 1){
    question2=prompt('qArray[counter]','');
    }
    if (counter == 2){
    question3=prompt('qArray[counter]','');
    }
    if (counter == 3){
    question4=prompt('qArray[counter]','');
    }
    counter++;
    }
    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]){
    score++;
    }
    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.


    </script>
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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:-

    Code:
    <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">
    <br><br>
    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">
    <br><br>
    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">
    <br><br>
    <input type = "button" name= "but1" value = "Check The Answers" onclick = "chkrads('myform')">
    </form>
    
    <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  ");
    }  
    
    </script>

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


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 07-30-2010 at 04:32 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Thanks

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Ag555 View Post
    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.

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


  •  

    Posting Permissions

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