surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.14k stars 806 forks source link

Quiz Question Bank #2089

Closed mtb-prgrmer closed 4 years ago

mtb-prgrmer commented 4 years ago

Are you requesting a feature, reporting a bug or asking a question?

Asking a question.

I have followed the documentation and examples for creating a quiz. Now, I am wondering if it is possible to choose quiz questions from a bank of questions randomly so that each time the quiz is taken there are different questions.

Please let me know if there is an existing way to do this or if there is some direction you could point me towards how I might implement this feature myself.

Specify your

tsv2013 commented 4 years ago

There is no built-in way to do it, but this can be achieved via JavaScript code.

You can store JSON objects - serialized to JSON questions. These objects can be stored as JSON in DB or as a serialized JSON strings. Here is the working sample - https://plnkr.co/edit/gnL4gv75uowDPyEU

    var questionJSONs = [
        {
            name: "name",
            type: "text",
            title: "Please enter your name:",
            placeHolder: "Jon Snow",
            isRequired: true
        }, {
            name: "birthdate",
            type: "text",
            inputType: "date",
            title: "Your birthdate:",
            isRequired: true
        }, {
            name: "color",
            type: "text",
            inputType: "color",
            title: "Your favorite color:"
        }, {
            name: "email",
            type: "text",
            inputType: "email",
            title: "Your e-mail:",
            placeHolder: "jon.snow@nightwatch.org",
            isRequired: true,
            validators: [
                {
                    type: "email"
                }
            ]
        }
    ];

var json = {
    pages: [{name: "page1"}]
};

window.survey = new Survey.Model(json);
var page = survey.pages[0];
questionJSONs.forEach(function(questionJson) {
    var question = page.addNewQuestion(questionJson.type, questionJson.name);
    question.fromJSON(questionJson);
});
mtb-prgrmer commented 4 years ago

@tsv2013

This is very helpful; however, I am relatively new to programming and am having some difficulty implementing this. I have decided to use some code like:

var randomNumber = Math.round(Math.random()*3);

So, say I have the sample question bank based on the example you provided above with four questions. Now, I want one of these questions to be randomly selected and added to survey. I use the randomNumber variable which randomly select a number from 0-3 which gives 4 options. Then, it should add one question based on the randomly generated number (0-3). How would I do this?

The example above uses a foreach loop but I'd like to not use a foreach loop because that would add every question. I only want to add one question and get that question based on the random number that I generate.

tsv2013 commented 4 years ago

It's a very basic programming task:

    var randomNumber = Math.round(Math.random()*3);
    var randomQuestionJson = questionJSONs[randomNumber];
    var question = page.addNewQuestion(randomQuestionJson.type, randomQuestionJson.name);
    question.fromJSON(randomQuestionJson);
mtb-prgrmer commented 4 years ago

Thank you! I think I was getting caught up on the syntax. I also was not real familar with JSON objects and how they work. Anyways, I was able to get a working example for this. Here is the code I came up with if anyone comes across this in the future and has the same issue:

https://plnkr.co/edit/I4vXNTFEgIP9zy1s?open=lib%2Fscript.js&preview