Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (68 loc) · 2.79 KB
/
Copy pathscript.js
File metadata and controls
81 lines (68 loc) · 2.79 KB
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
const techData = [
{ id: "t1", name: "React", category: "frontend" },
{ id: "t2", name: "Node.js", category: "backend" },
{ id: "t3", name: "Vue.js", category: "frontend" },
{ id: "t4", name: "Python", category: "backend" },
{ id: "t5", name: "PostgreSQL", category: "backend" },
{ id: "t6", name: "Svelte", category: "frontend" }
];
const inventory = document.getElementById("inventory-list");
const zones = document.querySelectorAll(".drop-zone");
const modal = document.getElementById("error-modal");
const modalMsg = document.getElementById("modal-message");
const closeBtn = document.getElementById("modal-close");
techData.sort(() => Math.random() - 0.5);
techData.forEach(function(item) {
const chip = document.createElement("div");
chip.className = "tech-chip";
chip.draggable = true;
chip.id = item.id;
chip.innerText = item.name;
chip.dataset.answer = item.category;
chip.addEventListener("dragstart", function(e) {
e.target.classList.add("dragging");
e.dataTransfer.setData("text", e.target.id);
});
chip.addEventListener("dragend", function(e) {
e.target.classList.remove("dragging");
});
inventory.appendChild(chip);
});
zones.forEach(function(zone) {
zone.addEventListener("dragover", function(e) {
e.preventDefault();
zone.classList.add("drag-over");
});
zone.addEventListener("dragleave", function(e) {
zone.classList.remove("drag-over");
});
zone.addEventListener("drop", function(e) {
e.preventDefault();
zone.classList.remove("drag-over");
const chipId = e.dataTransfer.getData("text");
const draggedChip = document.getElementById(chipId);
const chipAnswer = draggedChip.dataset.answer;
const zoneCat = zone.getAttribute("data-category");
if (chipAnswer === zoneCat) {
const content = zone.querySelector(".zone-content");
content.appendChild(draggedChip);
draggedChip.classList.add("correct-drop");
draggedChip.draggable = false;
const remaining = inventory.querySelectorAll(".tech-chip").length;
if (remaining === 0) {
setTimeout(function() {
modalMsg.innerHTML = "🎉 Level Complete! You sorted them all.";
closeBtn.innerText = "Play Again";
closeBtn.onclick = function() { location.reload(); };
modal.classList.add("show");
}, 300);
}
} else {
modalMsg.innerHTML = `<span style="font-weight:bold; color:#ef4444">${draggedChip.innerText}</span> belongs elsewhere!`;
modal.classList.add("show");
}
});
});
closeBtn.addEventListener("click", function() {
modal.classList.remove("show");
});