-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
342 lines (327 loc) · 10.6 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
let argArr = [];
let argArrString = [];
let jsx;
let jsx2;
let scriptTag;
let scriptTag2;
let applyFuncArguments;
let applyFuncArguments2;
let timeElapsed;
let timeElapsed2;
let timeElapsedArr = [];
let timeElapsedArr2 = [];
let inputArgs = [];
let setDisplay = false;
let editor2;
let error = false;
let error2 = false;
const submit = document.querySelector('.submit');
const errorMsg = document.querySelector('.errorMsg');
const errorMsg2 = document.querySelector('.errorMsg2');
const codearea = document.getElementById('codearea');
const codearea2 = document.getElementById('codearea2');
const addSolutionButton = document.querySelector('.add_2nd_solution');
const reset = document.querySelector('.reset');
const modal = document.getElementById('modal');
const showModal = document.getElementById('showModal');
const span = document.getElementById('close');
//-----------modal----------//
showModal.onclick = function() {
modal.style.display = 'block';
showModal.style.visibility = 'hidden';
};
span.onclick = function() {
modal.style.display = 'none';
showModal.style.visibility = 'visible';
};
//-----------generate chart----------//
function chart() {
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: argArr,
datasets: [
{
label: 'Time Elapsed Solution1',
data: timeElapsedArr,
borderColor: 'green',
fill: false
}
]
},
options: {
responsive: true,
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
],
xAxes: [
{
ticks: {
beginAtZero: true
}
}
]
},
tooltips: {
mode: 'point'
},
title: {
display: true,
text: 'BigO Chart Representation',
fontSize: 20,
fontColor: '#CBDAE5'
},
defaults: {
scale: {
ticks: { min: 0 }
}
},
events: ['click']
}
});
}
function chartTwoSolution() {
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: argArr,
datasets: [
{
label: 'Time Elapsed Solution1',
data: timeElapsedArr,
borderColor: 'green',
fill: false
},
{
label: 'Time Elapsed Solution2',
data: timeElapsedArr2,
borderColor: '#ef7b5f',
fill: false
}
]
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
],
xAxes: [
{
ticks: {
beginAtZero: true
}
}
]
},
tooltips: {
mode: 'point'
},
title: {
display: true,
text: 'BigO Chart Representation',
fontSize: 20,
fontColor: '#CBDAE5'
},
defaults: {
scale: {
ticks: { min: 0 }
}
},
events: ['click']
}
});
}
//---------codearea editor----------//
let editor = CodeMirror.fromTextArea(document.getElementById('codearea'), {
lineNumbers: true,
mode: 'javascript',
theme: 'lucario',
lineWrapping: true,
matchBrackets: true,
autoCloseTags: true
});
//---------codearea2 editor----------//
const changeDisplay = () => {
if (setDisplay === false) {
codearea2.style.display = 'block';
errorMsg2.style.display = 'block';
setDisplay = true;
editor2 = CodeMirror.fromTextArea(
document.getElementById('codearea2'),
{
lineNumbers: true,
mode: 'javascript',
theme: 'lucario',
lineWrapping: true,
matchBrackets: true,
autoCloseTags: true
}
);
chartTwoSolution();
addSolutionButton.innerHTML = 'hide 2nd solution';
submit.innerHTML = 'run solutions';
} else {
editor2.toTextArea();
codearea2.style.display = 'none';
errorMsg2.style.display = 'none';
setDisplay = false;
chart();
addSolutionButton.innerHTML = 'add a 2nd solution';
submit.innerHTML = 'run solution';
}
};
addSolutionButton.addEventListener('click', changeDisplay);
//--------------click handler on RUN--------------------//
let clickHandler = () => {
error = false;
error2 = false;
// e.preventDefault();
getFirstInput();
getSecondInput();
document.createElement('script').remove;
//get editor text value and ignore 'comments'
jsx = editor
.getValue()
.replace(/(?:\/\*(?:[\s\S]*?)\*\/)|([^\\:]|^)\/\/.*$/gm, '');
scriptTag = document.createElement('script');
scriptTag.setAttribute('id', 'toEval');
scriptTag.textContent = jsx;
if (editor2) {
jsx2 = editor2
.getValue()
.replace(/(?:\/\*(?:[\s\S]*?)\*\/)|([^\\:]|^)\/\/.*$/gm, '');
scriptTag2 = document.createElement('script');
scriptTag2.setAttribute('id', 'toEval2');
scriptTag2.textContent = jsx2;
}
try {
//----------create function from user input in codearea------------//
let body = scriptTag.textContent;
body = body.replace(/^\s*/gm, '');
if (body.length > 0) {
let wrap = () => '{ return ' + body + ' };'; //return the block having function expression
// const count_forLoops = (body.match(/for/g) || []).length;
let func = new Function(wrap(body));
applyFuncArguments = func.apply(null, argArrString);
//--------calculate time elapsed-------------
let t1 = performance.now();
let funcOutput = applyFuncArguments(inputArgs[0], inputArgs[1]);
let t2 = performance.now();
timeElapsed = t2 - t1;
timeElapsedArr.push(timeElapsed);
error
? (errorMsg.style.color = 'red')
: (errorMsg.style.color = 'green');
errorMsg.value = `Output: ${funcOutput}`;
}
} catch (err) {
errorMsg.value = 'OOPSY DAISY Error: ' + err.message;
error = true;
error
? (errorMsg.style.color = 'red')
: (errorMsg.style.color = 'green');
console.log(errorMsg);
}
try {
//----------create function from user input in codearea------------//
if (scriptTag2) {
let body2 = scriptTag2.textContent;
body2 = body2.replace(/^\s*/gm, '');
if (body2.length > 0) {
let wrap2 = () => '{ return ' + body2 + ' };'; //return the block having function expression
let func2 = new Function(wrap2(body2));
applyFuncArguments2 = func2.apply(null, argArrString);
//--------calculate time elapsed-------------
let _t1 = performance.now();
let _funcOutput = applyFuncArguments2(
inputArgs[0],
inputArgs[1]
);
let _t2 = performance.now();
timeElapsed2 = _t2 - _t1;
timeElapsedArr2.push(timeElapsed2);
error2
? (errorMsg2.style.color = 'red')
: (errorMsg2.style.color = '#ef7b5f');
errorMsg2.value = `Output: ${_funcOutput}`;
}
}
//---------------reset arg array length----------------------------//
argArrString.length = 0;
inputArgs.length = 0;
//----------------------draw chart-------------------------------//
setDisplay ? chartTwoSolution() : chart();
} catch (err) {
errorMsg2.value = 'OOPSY DAISY Error: ' + err.message;
error2 = true;
error2
? (errorMsg2.style.color = 'red')
: (errorMsg2.style.color = '#ef7b5f');
console.log(errorMsg);
}
};
//-----------get func args from input field-------------//
function getFirstInput() {
let inputVal = document.getElementById('arg1').value;
if (inputVal !== '' && Array.isArray(JSON.parse(inputVal)) === true) {
argArrString.push(inputVal);
let arrLength = calcArrLength(JSON.parse(inputVal));
argArr.push(arrLength);
inputArgs.push(JSON.parse(inputVal));
} else if (inputVal !== '' && typeof parseInt(inputVal) === 'number') {
argArrString.push(inputVal);
argArr.push(parseInt(inputVal));
inputArgs.push(parseInt(inputVal));
}
return argArr;
}
function getSecondInput() {
let inputVal = document.getElementById('arg2').value;
if (inputVal !== '' && Array.isArray(JSON.parse(inputVal)) === true) {
argArrString.push(inputVal);
let arrLength = calcArrLength(JSON.parse(inputVal));
argArr.push(arrLength);
inputArgs.push(JSON.parse(inputVal));
} else if (inputVal !== '' && typeof parseInt(inputVal) === 'number') {
argArrString.push(inputVal);
argArr.push(parseInt(inputVal));
inputArgs.push(parseInt(inputVal));
}
return argArr;
}
function calcArrLength(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (typeof parseInt(arr[i]) === 'number') {
count++;
} else if (Array.isArray(JSON.parse(arr[i]))) {
calcArrLength(JSON.parse(arr[i]));
} else {
alert(
'Please use only arrays and/or numbers as function arguments'
);
}
}
return count;
}
function resetChart() {
timeElapsedArr.length = 0;
timeElapsedArr2.length = 0;
setDisplay ? chartTwoSolution() : chart();
}
setDisplay ? chartTwoSolution() : chart();
submit.addEventListener('click', clickHandler);
reset.addEventListener('click', resetChart);