-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (62 loc) · 1.78 KB
/
Copy pathscript.js
File metadata and controls
76 lines (62 loc) · 1.78 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
const startBtn = document.getElementById("startBtn");
const rightBox = document.getElementById("gameArea");
const stopBtn = document.getElementById("stopBtn");
const stackBox = document.getElementById("stackBox");
const sortedBox = document.getElementById("sortedBox");
let stack=[];
let intervalId=null;
startBtn.addEventListener("click", function () {
if(intervalId)
return;
stack=[];
stackBox.innerHTML="";
sortedBox.innerHTML = "";
rightBox.innerHTML = "";
intervalId=setInterval(function () {
let bubble = document.createElement("div");
bubble.className = "bubble";
const maxLeft = rightBox.clientWidth - 60;
bubble.style.left = Math.floor(Math.random() * maxLeft) + "px";
bubble.addEventListener("mousedown", function() {
let num = Math.floor(Math.random() * 100) + 1;
stack.push(num);
let stackItem = document.createElement("div");
stackItem.innerText = num;
stackBox.appendChild(stackItem);
bubble.remove();
});
rightBox.appendChild(bubble);
setTimeout(function () {
bubble.remove();
}, 7000);
}, 600);
});
stopBtn.addEventListener("click",function (){
if(!intervalId)
return;
clearInterval(intervalId);
intervalId=null;
rightBox.innerHTML = "";
performBubbleSort();
displaySortedOutput();
});
function performBubbleSort(){
for(let i=0;i<stack.length;i++){
for(let j=0;j<stack.length;j++){
if(stack[j]>stack[j+1]){
let temp = stack[j];
stack[j] = stack[j + 1];
stack[j + 1] = temp;
}
}
}
}
function displaySortedOutput(){
stack.forEach((num, index) => {
setTimeout(() => {
let item = document.createElement("div");
item.innerText = num;
sortedBox.appendChild(item);
}, index * 300);
});
}