-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
70 lines (56 loc) · 2.13 KB
/
script.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
$(document).ready(function() {
$("#processCell").append(
'<button class="btn_go" id="btn_go">Go!</button><br>' +
'<textarea id="text_area"></textarea><br>' +
'<div id="debug"></div>');
for(var d=0;d<names.length;d++) {
$("#problems").append($("<option>").attr('value', d+1).text(names[d]));
}
$(".btn_go").click(function() {
$("#debug,#countDiv,#outputCell").empty();
processFunction($('#problems'));
});
});
function processFunction(select) {
$("#outputCell").append('<div class="outputHead"><span class="output1"><h2>Expected</h2></span><span style="width:30%" class="output2"><h2>Run</h2></span></div>');
var item = select.val();
var s = $("#text_area").val();
if(s[0]=='"' && s[s.length-1]=='"') {
s = stripQuotes($("#text_area").val());
}
try {
eval(s);
} catch(err) {
$("#debug").html(err);
}
var count = 0;
for (var x = 0; x < eval("test_"+item+".length"); x++) {
try {
var e = eval(eval("test_" + item + "[x]"));
} catch(err) {
$("#debug").html(err);
}
$("#outputCell").append(
"<div class='output'>" +
"<span class='output1'>" + eval("test_" + item + "[x]") + " → " + eval("answer_"+item+"[x]") + "</span>" +
"<span class='output2' id='output_" + x + "'>" + e + "</span>" +
"<span class='output3' id='outputImg_" + x + "'></span>" +
"</div>"
);
if (e.toString() == eval("answer_"+item+"[x].toString()")) {
$("#output_" + x).css("background", "forestgreen");
$("#outputImg_" + x).html("<img src='check.png' width='20'/>");
count++;
} else {
$("#output_" + x).css("background", "red");
$("#outputImg_" + x).html("<img src='x.png' width='20'/>");
}
}
$("#outputCell").append("<div id='countDiv'>" + count + " out of " + x + " correct.</div>");
}
function stripQuotes(str) {
//console.log("stripping");
str = str.substring(1,str.length-1);
str = str.replace(/""/g,'"')
return str;
}