-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
161 lines (139 loc) · 5.06 KB
/
main.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
window.onload=()=>{
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML += slider.value; // Display the default slider value
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
output.innerHTML = this.value;
}
setItems();
updateKnapsackText(true);
updateKnapsackText(false);
drawRecursionTree();
}
function setItems() {
for (let i = 0; i < weights.length; i++) {
let item = document.getElementById("item" + i);
item.setAttribute("weight", "" + weights[i]);
item.setAttribute("value", "" + vals[i]);
item.innerHTML = "Weight: " + weights[i] + ", Value: " + vals[i];
item.style.height = 40 * parseInt(item.getAttribute("weight")) - 4 + "px";
// let randomColor = Math.floor(Math.random()*16777215).toString(16);
item.style.backgroundColor = colorPicker(vals[i]);
let aitem = document.getElementById("aitem" + i);
outsideSet.add(aitem);
aitem.setAttribute("weight", "" + weights[i]);
aitem.setAttribute("value", "" + vals[i]);
aitem.innerHTML = "Weight: " + weights[i] + ", Value: " + vals[i];
aitem.style.height = 40 * parseInt(aitem.getAttribute("weight")) - 4 + "px";
aitem.style.backgroundColor = colorPicker(vals[i]);
}
}
function putInside(item, clickable) {
let item_weight = parseInt(item.getAttribute("weight"));
if (getKnapsackWeight(clickable) + item_weight <= weightLimit) {
let inside;
if (clickable) {
inside = document.getElementById("inside");
} else {
inside = document.getElementById("ainside");
}
inside.insertBefore(item, inside.firstChild);
if (clickable) {
item.setAttribute("onClick", "putOutside(this, true)");
} else {
item.style.width = 280 + "px";
item.style.fontSize = "medium";
}
}
let currValue = getKnapsackValue(clickable);
if (clickable) {
if (currValue > bestVal) {
bestVal = currValue;
updateKnapsackText(true);
}
} else {
if (currValue > abestVal) {
abestVal = currValue;
updateKnapsackText(false);
}
}
}
function getKnapsackWeight(clickable) {
let children;
if (clickable) {
children = document.getElementById("inside").childNodes;
} else {
children = document.getElementById("ainside").childNodes;
}
let weight = 0;
for (let i = 0; i < children.length; i++) {
weight += parseInt(children.item(i).getAttribute("weight"));
}
return weight;
}
function getKnapsackValue(clickable) {
let children;
if (clickable) {
children = document.getElementById("inside").childNodes;
} else {
children = document.getElementById("ainside").childNodes;
}
let value = 0;
for (let i = 0; i < children.length; i++) {
value += parseInt(children.item(i).getAttribute("value"));
}
return value;
}
function colorPicker(size) {
let rgbDark = [42, 103, 105];
let rgbLight = [153, 220, 222];
let ratio = size / 9;
let red = rgbLight[0] - Math.round((rgbLight[0] - rgbDark[0]) * ratio);
let green = rgbLight[1] - Math.round((rgbLight[1] - rgbDark[1]) * ratio);
let blue = rgbLight[2] - Math.round((rgbLight[2] - rgbDark[2]) * ratio);
let rgbToString = "rgb(" + red + ", " + green + ", " + blue + ")";
return rgbToString;
}
function selectJs() {
let jsButton = document.getElementById("jsButton");
let javaButton = document.getElementById("javaButton");
let pythonButton =document.getElementById("pythonButton");
let jsCode = document.getElementById("jsCode");
let javaCode = document.getElementById("javaCode");
let pythonCode = document.getElementById("pythonCode");
jsCode.style.display = "block";
javaCode.style.display = "none";
pythonCode.style.display = "none";
jsButton.style.backgroundColor = "orange";
javaButton.style.backgroundColor = "cadetblue";
pythonButton.style.backgroundColor = "cadetblue";
}
function selectJava() {
let jsButton = document.getElementById("jsButton");
let javaButton = document.getElementById("javaButton");
let pythonButton =document.getElementById("pythonButton");
let jsCode = document.getElementById("jsCode");
let javaCode = document.getElementById("javaCode");
let pythonCode = document.getElementById("pythonCode");
jsCode.style.display = "none";
javaCode.style.display = "block";
pythonCode.style.display = "none";
jsButton.style.backgroundColor = "cadetblue";
javaButton.style.backgroundColor = "orange";
pythonButton.style.backgroundColor = "cadetblue";
}
function selectPython() {
let jsButton = document.getElementById("jsButton");
let javaButton = document.getElementById("javaButton");
let pythonButton =document.getElementById("pythonButton");
let jsCode = document.getElementById("jsCode");
let javaCode = document.getElementById("javaCode");
let pythonCode = document.getElementById("pythonCode");
jsCode.style.display = "none";
javaCode.style.display = "none";
pythonCode.style.display = "block";
jsButton.style.backgroundColor = "cadetblue";
javaButton.style.backgroundColor = "cadetblue";
pythonButton.style.backgroundColor = "orange";
}