-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (81 loc) · 2.84 KB
/
app.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
"use strict";
const start = async () => {
let algoValue = Number(document.querySelector(".algo-menu").value);
let speedValue = Number(document.querySelector(".speed-menu").value);
if (speedValue === 0) {
speedValue = 1;
}
if (algoValue === 0) {
alert("No Algorithm Selected");
return;
}
let algorithm = new sortAlgorithms(speedValue);
if (algoValue === 1) await algorithm.BubbleSort();
if (algoValue === 2) await algorithm.SelectionSort();
if (algoValue === 3) await algorithm.InsertionSort();
if (algoValue === 4) await algorithm.MergeSort();
if (algoValue === 5) await algorithm.QuickSort();
};
const RenderScreen = async () => {
let algoValue = Number(document.querySelector(".algo-menu").value);
await RenderList();
};
const RenderList = async () => {
let sizeValue = Number(document.querySelector(".size-menu").value);
await clearScreen();
let list = await randomList(sizeValue);
const arrayNode = document.querySelector(".array");
console.log(arrayNode);
console.log(list);
for (const element of list) {
const node = document.createElement("div");
node.className = "cell";
node.setAttribute("value", String(element));
node.style.height = `${3.8 * element}px`;
arrayNode.appendChild(node);
}
};
const RenderArray = async (sorted) => {
let sizeValue = Number(document.querySelector(".size-menu").value);
await clearScreen();
let list = await randomList(sizeValue);
if (sorted) list.sort((a, b) => a - b);
const arrayNode = document.querySelector(".array");
const divnode = document.createElement("div");
divnode.className = "s-array";
for (const element of list) {
const dnode = document.createElement("div");
dnode.className = "s-cell";
dnode.innerText = element;
divnode.appendChild(dnode);
}
arrayNode.appendChild(divnode);
};
const randomList = async (Length) => {
let list = new Array();
let lowerBound = 1;
let upperBound = 100;
for (let counter = 0; counter < Length; ++counter) {
let randomNumber = Math.floor(
Math.random() * (upperBound - lowerBound + 1) + lowerBound
);
list.push(parseInt(randomNumber));
}
return list;
};
const clearScreen = async () => {
document.querySelector(".array").innerHTML = "";
};
const response = () => {
let Navbar = document.querySelector(".navbar");
if (Navbar.className === "navbar") {
Navbar.className += " responsive";
} else {
Navbar.className = "navbar";
}
};
document.querySelector(".icon").addEventListener("click", response);
document.querySelector(".start").addEventListener("click", start);
document.querySelector(".size-menu").addEventListener("change", RenderScreen);
document.querySelector(".algo-menu").addEventListener("change", RenderScreen);
window.onload = RenderScreen;