-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.js
106 lines (98 loc) · 2.2 KB
/
questions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var QUESTIONS = [
{
id: "wofuer",
title: "Wofür?",
question: "Wofür benutzt du das Auto?",
answers: {
"arbeit": {
name: "Arbeit",
next: ["pendeln"],
},
"familie": {
name: "Familienauto",
next: [],
}
},
},
{
id: "pendeln",
title: "Pendeln?",
question: "Pendelst du mit dem Auto?",
answers: {
"ja": {
name: "Ja",
next: [],
},
"nein": {
name: "Nein",
next: [],
}
},
},
{
id: "test",
title: "Test",
question: "FDSfsdfsdfsdf",
answers: {
"test": {
name: "test",
next: [],
}
}
}
];
var DEFAULT_QUESTIONS = ["wofuer", "test"];
function getQuestion(id) {
for (i in QUESTIONS)
if (QUESTIONS[i].id == id)
return QUESTIONS[i];
}
function displayQuestion(question) {
console.log("displayQuestion", question);
$("#question").html(question.question);
$("#answers").empty();
for (id in question.answers) {
var answer = question.answers[id];
var div = $("<div class='checkbox'></div>");
var label = $("<label></label>");
var input = $("<input />");
input.attr("type", "checkbox");
input.data("id", id);
label.append(input);
label.append(answer.name);
div.append(label);
$("#answers").append(div);
}
}
var questions = DEFAULT_QUESTIONS;
var currentQuestion = null;
var answers = {};
function nextQuestion() {
if (questions.length == 0) {
alert("Fertig!");
console.log(answers);
return;
}
currentQuestion = getQuestion(questions.shift());
displayQuestion(currentQuestion);
}
$(window).ready(function() {
nextQuestion();
$("#next").click(function() {
var currentAnswers = [];
$(".checkbox input:checked").each(function(index) {
var id = $(this).data("id");
currentAnswers.push(id);
var answer = currentQuestion.answers[id];
for (i in answer.next) {
var id = answer.next[i];
if (!(id in answers) && questions.indexOf(id) == -1) {
console.log("unshift", id);
questions.unshift(id);
}
}
});
answers[currentQuestion.id] = currentAnswers;
nextQuestion();
});
});