-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_execution.js
More file actions
70 lines (65 loc) · 1.89 KB
/
Copy pathcode_execution.js
File metadata and controls
70 lines (65 loc) · 1.89 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
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
document.getElementById('executeButton').addEventListener('click', function() {
var userCode = editor.getValue();
var wrappedCode = `
var array = [64, 34, 25, 12, 22, 11, 90];
${userCode}
visualizeSorting(array);
`;
try {
eval(wrappedCode);
} catch (e) {
document.getElementById('output').textContent = e.message;
}
});
function visualizeSorting(array) {
const container = document.getElementById('visualizationContainer');
container.innerHTML = '';
array.forEach(value => {
const cell = document.createElement('div');
cell.className = 'cell';
cell.textContent = value;
cell.style.backgroundColor = getRandomColor();
container.appendChild(cell);
});
updateChart(array);
}
function updateChart(array) {
const ctx = document.getElementById('chartContainer').getContext('2d');
if (window.myChart) {
window.myChart.destroy();
}
window.myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: array.map((_, i) => i),
datasets: [{
label: 'Array Values',
data: array,
backgroundColor: array.map(() => getRandomColor()),
borderColor: array.map(() => getRandomColor()),
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}