1

I need some help with a code for a multiple choice quiz, but it also needs true/false options. I have a code that makes a quiz for a multiple choice questions with 3 options, that's fine. But when i want a true/false question with 2 options, it won't hide the last option, instead is shows the third options from the previous question. I tried almost everything but i couldn't figure out how i can hide the third option in a t/f question. The following code is the code i use. The word "yolo" in a option stands for the options that should be hided. Is it possible to hide?

$(function() {

var currentQuestion = null;
var questionIndex = 0;

// Your list of questions. Each question has an answer (either a,b or c)
// and then a set of "options" in the question
var questions = [

  { 
      'answer': 'a',
      'question': 'Wat is de correcte syntaxis in een HTML om te verwijzen naar een extern JS bestand:',
      options: ['<script src="xxx.js">', '<script name="xxx.js">', '<script href="xxx.js">']
  },
  { 
      'answer': 'c',
      'question': 'Welk teken is het teken voor JQuery in JQuery:',
      options: ['Het ? teken', 'Het % teken', 'Het $ teken']
  },
  {
      'answer': 'b',
      'question': 'Wat is JQuery van JavaScript?',
      options: ['Een uitbreiding (meer mogeliijkheden)', 'Een simpele versie (minder codes voor meer)', 'Hetzelfde (alleen de titel is anders)']
  },
  {
      'answer': 'b',
      'question': 'JQuery kan je direct gebruiken in een HTML bestand?',
      options: ['Juist','Onjuist', 'yolo']
  },

];

// Detect when the submit button is clicked and check if the question
// was answered correctly
$('input[type="submit"]').click(function() {
    var val = $('#questions').find('input:checked').val();
    if(currentQuestion) {
        if(currentQuestion.answer == val) {
            alert("Goed zo!");
            showQuestion();
        } else {
            alert("Jammer, probeer het nog eens.");
        }
    }
    return false;
});

// Set the value of an option in the question
function setRadioLabel(radioId, text) {
     $('label[for="' + radioId + '"]').find('span.ui-btn-text').text(text);
};

// Show a random question
function showQuestion() {
    // Grab next question, and increment so we get a new one next time
    var random = questions[questionIndex++ % questions.length];

    $('#question').text(random.question);

    $('input[type="radio"]').attr('checked', false).checkboxradio('refresh');

    setRadioLabel('radio1', random.options[0]);
    setRadioLabel('radio2', random.options[1]);
    setRadioLabel('radio3', random.options[2]);
    currentQuestion = random;
};

// Start the question stuff off
showQuestion();
});

Another issue. The quiz never ends, when the quiz had all the questions, it starts again with question 1. Is there something that could end the quiz with the last question?

The quiz (questions and options) is in Dutch. It is also my first question i post, so it may not be correct (right place and way and that sort of things).


I forgot to say that the JQuery version is 1.9.1 en the JQueryMobile version is 1.3.1. For some reason it still wont work. I tried the 3 answers as far as i understood them. Could someone edit the whole file in the way he (or she) thinks it should work.

E_onlinePC
  • 11
  • 1
  • 4
  • Totally tangential: 'true' and 'false' can also be looked at as just another 'multiple-choice', for this kind of thing. – Andrew Barber Jan 26 '15 at 11:04

2 Answers2

0

You can change your radio labels code to a loop, then it can handle any number of options (2,3,4,100...). In this way, you don't need the 'yolo' option, you can just set the options list to the number of options you want to provide.

for(var i = 0; i < random.options.length; i++)
{
  setRadioLabel('radio' + i.toString(), random.options[i]);
}

Not sure if the syntax is exactly right, it's completely untested and it's fairly close to what you need :)

For the other point, you haven't got anything in there to stop loading questions. Every time the page loads (on initial load and after each submit) it picks a question and displays it. You could maybe put a check in for if questionIndex % questions.length is 0 then show a Test Complete message instead of displaying the question. Or just remove the mod operator to ensure you only go through the list of questions once and put a condition in to control it:

function showQuestion() {
  // Grab next question, and increment so we get a new one next time
  if (questionIndex < questions.length) {
    var random = questions[questionIndex++];

    $('#question').text(random.question);

    $('input[type="radio"]').attr('checked', false).checkboxradio('refresh');

    for(var i = 0; i < random.options.length; i++)
    {
      setRadioLabel('radio' + i.toString(), random.options[i]);
    }
    currentQuestion = random;
    return;
  }
  // TODO: Code to show a Test Complete message and score here

};
cf_en
  • 1,661
  • 1
  • 10
  • 18
  • It shows the following error: Uncaught SyntaxError: Unexpected identifier. I don't know wich syntaxes i have to use either. Can you explain what you are doing with the code. – E_onlinePC Jan 27 '15 at 16:01
  • Sorry, I wasn't thinking about javascript type syntax. Changed int to var, and it should work. I've edited the answer for you. – cf_en Jan 27 '15 at 16:31
  • I tried your edited version, but it gives me the following error: Uncaught Error: cannot call methods on checkboxradio prior to initialization; attempted to call method 'refresh'. I am not sure what it means, but i got the same error before i asked the question, and then the message didn't always occure. – E_onlinePC Jan 27 '15 at 16:39
  • There are several questions already raised around that error that may help you with the use of checkboxradio('refresh'). Here is just one of many Google would return: http://stackoverflow.com/questions/12691780/jquery-mobile-uncaught-cannot-call-methods-on-checkboxradio-prior-to-initializa – cf_en Jan 27 '15 at 16:51
  • I just have to copy the same row above it and just delete refresh from the top one? – E_onlinePC Jan 27 '15 at 16:55
  • That's what it appears to be saying, but I'm afraid I'm not aware enough of the issue to be able to say that that is the solution and it won't cause any other problems :) – cf_en Jan 27 '15 at 17:08
  • That refresh thing is solved and now i was able to test your version. The problem now is that every question has 3 options and the last two options (second and third) are every question the same, it shows the last two options from the first question. And every question has another problem now. the first option from the first question is the first option from the first, the 2nd question has as 1st option the 2nd option, the 3rd question has as 1st option the 2nd option and the 4th question has as 1st option the 2nd option from question 3. After all question is stays at the same page. – E_onlinePC Jan 28 '15 at 16:23
  • I've amended the loop condition to i < random.options.length (removed the -1 part which was logically incorrect. The radios that are not applicable should be hidden, so if there are only two options hide the third, etc. You can do this with another for.. loop that loops repeats from random.options.length to the number of radio buttons and hides each, but on the next page load you might need to make them visible again. – cf_en Jan 29 '15 at 09:30
0

Start with formatting your json Object. Your keys shouldnt be strings:

var questions = [

  { 
      answer: 'a',
      question: 'Wat is de correcte syntaxis in een HTML om te verwijzen naar een extern JS bestand:',
      options: ['<script src="xxx.js">', '<script name="xxx.js">', '<script href="xxx.js">']
  },
  { 
      answer: 'c',
      question: 'Welk teken is het teken voor JQuery in JQuery:',
      options: ['Het ? teken', 'Het % teken', 'Het $ teken']
  },
  {
      answer: 'b',
      question: 'Wat is JQuery van JavaScript?',
      options: ['Een uitbreiding (meer mogeliijkheden)', 'Een simpele versie (minder codes voor meer)', 'Hetzelfde (alleen de titel is anders)']
  },
  {
      answer: 'b',
      question: 'JQuery kan je direct gebruiken in een HTML bestand?',
      options: ['Juist','Onjuist', 'yolo']
  }


];

Answering your second question, you can use splice method to remove used objects from array:

questions.splice((Math.floor(Math.random()*(questions.length))),1);
console.log(questions);

It removes item at random index in range [0, size of your array]

Put your whole code with js and html in jsFidle (http://jsfiddle.net/) including above solution if you need more help

Mike
  • 447
  • 4
  • 15