M
M
Minningt2015-10-05 22:31:16
JavaScript
Minningt, 2015-10-05 22:31:16

How to make a test?

Hello everyone. How to make an easy test if the questions are known and it is necessary to answer "yes / no" and depending on the answer another question will appear with the same answers. Tree type. And you need to hide all the questions and leave the very first
jsfiddle.net/60t31z9g
For example, are you a football player? if so, show the question "Career"? Hide the previous question. etc

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2015-10-05
@dzheka3d

jsfiddle.net/9y478uh9

D
Denis, 2015-10-06
@Deonisius

tree type
If "tree type", then in my understanding - questions should be stored in some object. Like this:
var q = {
    'Question 1' : {
        'Yes' : {
            'Question 1.1' : {
                'Yes' : 'Result',
                'No' : { /* ... */ }
            }
        },
        'No' : {
            'Question 1.2' : { /* ... */ }
        }
    }
};
You did not specify this moment, but if I guessed it, then the option could be like this - jsfiddle.net/77ooxgwv
var q = {
    'Вы ненормальный?': {
        1: 'Windows Phone',
        0: {
            'Вы гей?': {
                1: 'Apple',
                0: {
                    'Цвет смартфона розовый?': {
                        1: 'Apple',
                        0: 'Android'
                    }
                }
            }
        }
    }
},
tmpl = '<p>@</p><input type="radio" name="ans" value="1">Да <input type="radio" name="ans" value="0">Нет',
cur = q[Object.keys(q)[0]],
out = $('output').html(tmpl.replace(/@/, Object.keys(q)[0]));

out.on('change', ':radio', function () {
    cur = cur[$(this).val()];
    if (typeof cur === 'string') {
        out.html('Вам подойдёт <b>' + cur + '</b><br>Тест окончен!');
    } else {
        out.html(tmpl.replace(/@/, Object.keys(cur)[0]));
        cur = cur[Object.keys(cur)[0]];
    }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question