-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (133 loc) · 3.8 KB
/
script.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
let left_col = ['every mouth',
'every hand',
'every chest',
'every body',
'every step',
'every knee',
'every thigh',
'every finger',
'every eye',
'every smile',
'every kiss',
'every ear',
'every nose',
'every back',
'every brain',
'every hip',
'every soul',
'every heartbeat',
'every breath']
let right_col = ['an oubliette',
'a promise',
'a sack of cats',
'a river',
'an exit',
'the shape of prayer',
'a pantry of lust',
'a name',
'a sin',
'a scythe',
'a nesting doll',
'a glutton',
'a wolf',
'a beast of burden',
'a whip',
'a thirst for touch',
'a currency',
'a stumble',
'a pearly gate']
let left_list = document.getElementById("left")
let right_list = document.getElementById("right")
left_col.forEach((item, i) => {
let li = document.createElement("li");
li.innerHTML = item;
li.setAttribute("draggable", "true");
li.setAttribute("id", "l" + i);
left_list.appendChild(li);
})
var original_list = []
right_col.forEach((item, i) => {
let li = document.createElement("li");
li.innerHTML = item;
li.setAttribute("draggable", "true");
li.setAttribute("id", "r" + i);
original_list.push(li);
})
randomize(original_list);
original_list.forEach(item => {
right_list.appendChild(item);
})
// Fisher–Yates shuffle Algorithm| source: https://www.geeksforgeeks.org/shuffle-a-given-array-using-fisher-yates-shuffle-algorithm/
function randomize(arr) {
// Start from the last element and swap
// one by one. We don't need to run for
// the first element that's why i > 0
for (let i = arr.length - 1; i > 0; i--) {
// Pick a random index from 0 to i inclusive
let j = Math.floor(Math.random() * (i + 1));
// Swap arr[i] with the element
// at random index
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
// Coding the drag behavior
const draggableListItems = document.querySelectorAll('.draggable-list li');
const endMessage = document.getElementById('endMessage');
//current phrase beng dragged
let selectedId;
//target phrase
let dropTargetId;
// counter for correct phrases
let matchingCounter = 0;
function dragStart() {
selectedId = this.id;
console.log(selectedId);
}
function dragEnter() {
this.classList.add("over");
}
function dragLeave() {
this.classList.remove("over");
}
function dragOver(ev) {
ev.preventDefault();
}
function dragDrop() {
dropTargetId = this.id;
console.log(dropTargetId);
this.classList.remove("over");
if (checkForMatch(selectedId, dropTargetId)) {
document.getElementById(selectedId).style.display = 'none';
document.getElementById(dropTargetId).style.display = 'none';
matchingCounter++;
}
if (matchingCounter === right_col.length) {
endMessage.style.display = "block";
}
}
function checkForMatch(selected, dropTarget) {
// check if the id of selected is equl to dropTarget
// extract the numerical part of the selected/target ids
if (selected[0] != dropTarget[0]) {
selected_num_id = selected.slice(1);
drop_num_id = dropTarget.slice(1);
return selected_num_id == drop_num_id ? true : false;
}
}
addEventListeners();
function playAgain() {
matchingCounter = 0;
endMessage.style.display = 'none';
draggableListItems.forEach(item => {
document.getElementById(item.id).style.display = 'block';
})
}
function addEventListeners() {
draggableListItems.forEach(item => {
item.addEventListener('dragstart', dragStart);
item.addEventListener('dragenter', dragEnter);
item.addEventListener('drop', dragDrop);
item.addEventListener('dragover', dragOver);
item.addEventListener('dragleave', dragLeave);
});
}