-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
103 lines (93 loc) · 3.26 KB
/
main.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
import {data} from "./data.js";
const options = document.querySelector('.options');
const answersList = document.querySelector('.answers ol');
const reloadBtn = document.querySelector('.reload-btn');
// const secondTen = document.querySelector('.secondTen');
// const thirdTen = document.querySelector('.thirdTen');
let shuffledData = shuffle(data);
let newData = shuffledData.slice(0, 9);
setAnswers(newData);
// setAnswers(data);
reloadBtn.addEventListener('click', reloadPage);
function reloadPage () {
location.reload();
}
// firstTen.addEventListener('click', chooseFirstTen);
// secondTen.addEventListener('click', chooseSecondTen);
// thirdTen.addEventListener('click', chooseThirdTen);
// function chooseFirstTen() {
// let newData = data.slice(0, 9);
// setAnswers(newData);
// };
// function chooseSecondTen() {
// let newData = data.slice(10, 19);
// setAnswers(newData);
// };
// function chooseThirdTen() {
// let newData = data.slice(20, data.length);
// setAnswers(newData);
// };
function setAnswers(arr) {
answersList.innerHTML = '';
options.innerHTML = '';
arr.forEach(function (item) {
options.insertAdjacentHTML('beforeend', `
<li class="option" data-target="${item.id}">${item.title}</li>
`);
});
shuffle(arr);
arr.forEach(function (item) {
answersList.insertAdjacentHTML('beforeend', `
<li><span class="target" data-accept="${item.id}"> </span>: ${item.description}</li>
`);
});
};
//тасование фишера-йетса
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1)); // случайный индекс от 0 до i
[array[i], array[j]] = [array[j], array[i]];
};
return array;
}
$(document).ready(function () {
//initialize the quiz options
var answersLeft = [];
$('.quiz-wrapper').find('li.option').each(function (i) {
var $this = $(this);
var answerValue = $this.data('target');
var $target = $('.answers .target[data-accept="' + answerValue + '"]');
var labelText = $this.html();
$this.draggable({
revert: "invalid",
containment: ".quiz-wrapper"
});
if ($target.length > 0) {
$target.droppable({
accept: 'li.option[data-target="' + answerValue + '"]',
drop: function (event, ui) {
$this.draggable('destroy');
$target.droppable('destroy');
$this.html(' ');
$target.html(labelText);
answersLeft.splice(answersLeft.indexOf(answerValue), 1);
}
});
answersLeft.push(answerValue);
} else { }
});
$('.quiz-wrapper button[type="submit"]').click(function () {
if (answersLeft.length > 0) {
$('.lightbox-bg').show();
$('.status.deny').show();
$('.lightbox-bg').click(function () {
$('.lightbox-bg').hide();
$('.status.deny').hide();
$('.lightbox-bg').unbind('click');
});
} else {
$('.lightbox-bg').show();
$('.status.confirm').show();
}
});
});