-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.js
48 lines (40 loc) · 1.56 KB
/
java.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
let arr = [50, 20, 15, 65, 10, 5];
let bubble = document.querySelector('.Bubble');
let btn = document.querySelector('.bot');
for (let i = 0; i < arr.length; i++) {
let innerdev = document.createElement("div");
innerdev.style.height = arr[i] * 5 + "px";
innerdev.style.backgroundColor = '#' + ((1 << 24) * Math.random() | 1).toString(16);
innerdev.setAttribute('id', 'elem' + i);
innerdev.classList.add("innerdev");
bubble.appendChild(innerdev);
}
btn.addEventListener("click", () => myfunction(arr)); // Attach the event listener to the button
const sleep = (time) => {
return new Promise(resolve => setTimeout(resolve, time));
}
async function myfunction(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < (arr.length - i - 1); j++) {
await sleep(800);
if (arr[j] > arr[j + 1]) { // Corrected the comparison for descending order
let temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
swapBarProperties(j);
}
}
}
}
function swapBarProperties(j) {
let a = 'elem' + j;
let b = 'elem' + (j + 1);
const bar1 = document.getElementById(a);
const bar2 = document.getElementById(b);
const tempHeight = bar1.style.height;
const tempColor = bar1.style.backgroundColor;
bar1.style.height = bar2.style.height;
bar1.style.backgroundColor = bar2.style.backgroundColor;
bar2.style.height = tempHeight;
bar2.style.backgroundColor = tempColor;
}