-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1980 lines (1834 loc) · 105 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Syntax Excercise
* Authors: Amal Nanavati, Ashley Wong, Emma Zhong
* AndrewIDs: arnanava, jzhong1, ashleywo
*/
/* runs the game
*/
var main = function (ex) {
//ex.data.meta.mode = "practice";
//ex.data.instance.state = {};
ex.unload(function(){
saveData();
});
/* 0 = code with dropdown
* 1 = code with click-the-error
* 2 = sode with different whitespaces
*/
var questionType = 0;
/* Which question number we are on (useful if we only want 10 questions) */
var questionNum = 1;
var totalNumOfQs = 9;
/* used in question type 1 */
var buttonList = undefined;
var buttonInfo = undefined;
var buttonCode = undefined;
var buttonCodeWell = undefined;
var buttonHeader = undefined;
var buttonDict = undefined;
var pressedButtons = [];
var indexPressedButtons = [];
var correctAnswers = undefined;
var q1OldAnswers = [];
var q1OldColors = [];
/* Used in question type 0, this is a list of dropdowns and the correct
* answer for each dropdown, the code well, and the header
*/
var dropdownList = undefined;
var question0CodeWell = undefined;
var question0Code = undefined;
var header = undefined;
var dropdownInfo = undefined;
var score = 0;
var totalPossibleScore = 0;
var endOfExercise = false;
/* Keeps track of whether the correct answer is being displayed or not
*/
var isCorrectAnswerBeingDisplayed = false;
var wasSubmitButtonClicked = false;
var oldAnswers = []; // Used in Q type 0 to restore old answers after unshowing CA
var oldColors = []; // Used in Q type 0 to restore old colors after unshowing CA
//The alert that will show feedback
var feedbackAlert = undefined;
var isFeedbackShowing = false;
var feedbackText = undefined;
var nextButton = undefined;
/* Used in Q type 2*/
var leftCode1 = "def isMultipleOfFourLeft(n):\n"
+" result = False\n"
+" if n%4 == 0:\n"
+" result = True\n"
+" return result";
var rightCode1 = "def isMultipleOfFourRight(n):\n"
+" result = False\n"
+" if n%4 == 0:\n"
+" result = True\n"
+" return result";
var leftCode2 = "def printPrimesLeft(n):\n"
+" for i in range(2,n):\n"
+" j = 2\n"
+" while(j <= (i/j)):\n"
+" if (i % j == 0): break\n"
+" j = j + 1\n"
+" if (j > i/j) : print i";
var rightCode2 = "def printPrimesRight(n):\n"
+" for i in range(2,n):\n"
+" j = 2\n"
+" while(j <= (i/j)):\n"
+" if (i % j == 0): break\n"
+" j = j + 1\n"
+" if (j > i/j) : print i";
/*var code1 = "def f(x):\n if (x == 2):\n x+=2\n return x";
var code2 = "def f(x):\n if (x == 2):\n x+=2\n return x";*/
var type2List = [IndentPrac(leftCode1,rightCode1,
"calculates if an integer is a multiple of 4",1),
IndentPrac(leftCode2,rightCode2,
"print all the prime numbers up to n",1)];
var question2qtype = 0;
var vars = createCode(2,0);
var currentIndentPrac = IndentPrac(vars.leftCode,vars.rightCode,
vars.question,vars.ca,vars.hint);
//saveData();
var currentIndex = 0;
/* Shows the appropriate question type
*/
var showQuestion = function () {
// If quiz mode, write which question you are on
if (ex.data.meta.mode == "quiz-immediate") {
var title = "Question ".concat(String(questionNum)).concat(" of ").concat(String(totalNumOfQs));
if (ex.chromeElements.titleHeader === undefined) ex.setTitle(title);
else ex.chromeElements.titleHeader.text(title)
};
console.log("show question");
switch (questionType) {
case 0:
/* Create the code well */
var codeInfo = createCode(questionType);
question0Code = codeInfo.code;
dropdownInfo = codeInfo.dropdownInfo;
var descr = codeInfo.descr;
/* Create the instructions/description of code */
header = ex.createParagraph(0,0,descr,{size: "xlarge", width:ex.width(), textAlign:"center"});
var margin = 50;
var x = 50;
var y = header.height()+margin;
var width = ex.width()-x-margin;
var height = ex.height()-y-margin;
var showFeedbackBool = (ex.data.meta.mode != "quiz-immediate");
var dict = createCodeWellWithOptions(ex, x, y, width, height, question0Code, dropdownInfo, showFeedbackBool, showFeedbackBool, true, saveData, showFeedback);
dropdownList = dict["dropdown"];
question0CodeWell = dict["code"];
console.log(question0CodeWell);
break;
case 1:
var codeInfo = createCode(questionType);
buttonCode = codeInfo.code;
buttonInfo = codeInfo.buttonInfo;
var descr = codeInfo.descr;
buttonDict = codeInfo.buttonDict;
correctAnswers = codeInfo.correctAnswers;
buttonHeader = ex.createParagraph(0, 0, descr, {size : "xlarge", width : ex.width(), textAlign : "center"});
var margin = 50;
var x = 50;
var y = buttonHeader.height() + margin;
var width = ex.width() - x - margin;
var height = ex.height() - y - margin * 2;
var showFeedbackBool = (ex.data.meta.mode != "quiz-immediate");
var dict = clickableCodeWell(x, y, width, height, buttonCode, buttonInfo, buttonDict, pressedButtons, showFeedbackBool, showFeedbackBool, true);
buttonList = dict["button"];
console.log(buttonList);
buttonCodeWell = dict["code"];
for (var i = 0; i < buttonList.length; i++) {
if (buttonList[i]["correct"] == true) {
buttonList[i]["button"].index = i;
buttonList[i]["button"].feedback = buttonList[i]["feedback"];
buttonList[i]["button"].on("click", function() {
var index = indexPressedButtons.indexOf(this.index);
if (index > -1) {
pressedButtons.splice(index,1);
indexPressedButtons.splice(index,1);
this.style({color : "white"});
}
else {
pressedButtons.push(true);
indexPressedButtons.push(this.index);
this.style({color : "blue"});
if (showFeedbackBool) showFeedback(this.feedback, true);
}
saveData();
});
}
else {
buttonList[i]["button"].index = i;
buttonList[i]["button"].feedback = buttonList[i]["feedback"];
buttonList[i]["button"].on("click", function() {
var index = indexPressedButtons.indexOf(this.index);
if (index > -1) {
pressedButtons.splice(index, 1);
indexPressedButtons.splice(index,1);
this.style({color : "white"});
}
else {
pressedButtons.push(false);
indexPressedButtons.push(this.index);
this.style({color : "blue"});
if (showFeedbackBool) showFeedback(this.feedback, false);
}
saveData();
});
}
}
break;
case 2:
/* delete and draw */
ex.graphics.ctx.clearRect(0,0,ex.width(),ex.height());
var showFeedbackBool = (ex.data.meta.mode != "quiz-immediate");
currentIndentPrac.draw();
if (currentIndentPrac.submitted){
currentIndentPrac.leftCard.well.off("click");
currentIndentPrac.rightCard.well.off("click");
currentIndentPrac.submit();
}
question2qtype = (question2qtype+1) % 2
break;
};
saveData();
};
var saveData = function (q0Option, q0DropdownI) {
var data = {};
data.questionType = questionType;
data.questionNum = questionNum;
data.totalNumOfQs = totalNumOfQs;
data.score = score;
data.totalPossibleScore = totalPossibleScore;
data.isCorrectAnswerBeingDisplayed = isCorrectAnswerBeingDisplayed;
data.wasSubmitButtonClicked = wasSubmitButtonClicked;
data.isUndoButtonDisabled = ex.chromeElements.undoButton.isDisabled();
data.isRedoButtonDisabled = ex.chromeElements.redoButton.isDisabled();
data.isResetButtonDisabled = ex.chromeElements.resetButton.isDisabled();
data.isNewButtonDisabled = ex.chromeElements.newButton.isDisabled();
data.isDisplayCAButtonDisabled = ex.chromeElements.displayCAButton.isDisabled();
data.isSubmitButtonDisabled = ex.chromeElements.submitButton.isDisabled();
data.isFeedbackShowing = isFeedbackShowing;
data.feedbackText = feedbackText;
console.log(feedbackText);
data.endOfExercise = endOfExercise;
if (endOfExercise) {
ex.saveState(data);
return;
}
switch (questionType) {
case 0:
console.log(q0Option);
console.log(q0DropdownI);
data.question0Code = question0Code;
data.dropdownInfo = jQuery.extend(true, {}, dropdownInfo);
data.descr = header.text();
data.oldAnswers = oldAnswers;
data.oldColors = oldColors;
data.dropdownDefaults = [];
//Save dropdown selected text index, extract it from the html
for (var i = 0; i < dropdownList.length; i++) {
var label;
if (i == q0DropdownI) {
label = q0Option;
} else {
label = dropdownToDefaultLabel(dropdownList[i].dropdown);
}
data.dropdownDefaults.push(label);
};
console.log(data);
break;
case 1:
data.buttonCode = buttonCode;
data.buttonInfo = jQuery.extend(true, {}, buttonInfo);
data.buttonDict = jQuery.extend(true, {}, buttonDict);
data.descr = buttonHeader.text();
data.pressedButtons = pressedButtons;
data.indexPressedButtons = indexPressedButtons;
data.correctAnswers = correctAnswers;
data.q1OldAnswers = q1OldAnswers;
data.q1OldColors = q1OldColors;
break;
case 2:
data.question2submitted = currentIndentPrac.submitted;
data.question2qtype = question2qtype;
data.question2LeftCode = currentIndentPrac.leftCode;
data.question2RightCode = currentIndentPrac.rightCode;
data.question2Question = currentIndentPrac.question;
data.question2Ca = currentIndentPrac.ca;
data.question2Clicked = currentIndentPrac.clicked;
//console.log(data.question2LeftCode);
};
ex.saveState(data);
console.log(ex.data.instance.state);
};
var loadData = function () {
console.log(ex.data.instance.state);
if (ex.data.instance.state != null && ex.data.instance.state != undefined && typeof(ex.data.instance.state) == "object" && Object.keys(ex.data.instance.state).length > 0) {
questionType = ex.data.instance.state.questionType;
questionNum = ex.data.instance.state.questionNum;
totalNumOfQs = ex.data.instance.state.totalNumOfQs;
score = ex.data.instance.state.score;
totalPossibleScore = ex.data.instance.state.totalPossibleScore;
isCorrectAnswerBeingDisplayed = ex.data.instance.state.isCorrectAnswerBeingDisplayed;
wasSubmitButtonClicked = ex.data.instance.state.wasSubmitButtonClicked;
//Re-disable or enable buttons
var disableOrEnable = function(element, isDisabled) {
if (isDisabled) {
element.disable();
} else {
element.enable();
};
};
disableOrEnable(ex.chromeElements.undoButton, ex.data.instance.state.isUndoButtonDisabled);
disableOrEnable(ex.chromeElements.redoButton, ex.data.instance.state.isRedoButtonDisabled);
disableOrEnable(ex.chromeElements.resetButton, ex.data.instance.state.isResetButtonDisabled);
disableOrEnable(ex.chromeElements.newButton, ex.data.instance.state.isNewButtonDisabled);
disableOrEnable(ex.chromeElements.displayCAButton, ex.data.instance.state.isDisplayCAButtonDisabled);
disableOrEnable(ex.chromeElements.submitButton, ex.data.instance.state.isSubmitButtonDisabled);
isFeedbackShowing = ex.data.instance.state.isFeedbackShowing;
feedbackText = ex.data.instance.state.feedbackText;
if (isFeedbackShowing) showFeedback(feedbackText);
endOfExercise = ex.data.instance.state.endOfExercise;
console.log(endOfExercise);
if (endOfExercise) {
var title = "End";
if (ex.chromeElements.titleHeader === undefined) {
ex.setTitle(title);
} else {
ex.chromeElements.titleHeader.text(title);
};
return true;
};
//Display next button if necessary
if (wasSubmitButtonClicked) createNextButton();
// If quiz mode, write which question you are on
if (ex.data.meta.mode == "quiz-immediate") {
var title = "Question ".concat(String(questionNum)).concat(" of ").concat(String(totalNumOfQs));
if (ex.chromeElements.titleHeader === undefined) ex.setTitle(title);
else ex.chromeElements.titleHeader.text(title);
};
switch (questionType) {
case 0:
if (ex.data.instance.state.isCorrectAnswerBeingDisplayed !== undefined) {
question0Code = ex.data.instance.state.question0Code;
dropdownInfo = ex.data.instance.state.dropdownInfo;
var descr = ex.data.instance.state.descr;
oldAnswers = ex.data.instance.state.oldAnswers;
oldColors = ex.data.instance.state.oldColors;
/* Create the instructions/description of code */
header = ex.createParagraph(0,0,descr,{size: "xlarge", width:ex.width(), textAlign:"center"});
var margin = 50;
var x = 50;
var y = header.height()+margin;
var width = ex.width()-x-margin;
var height = ex.height()-y-margin;
var showFeedbackBool = (ex.data.meta.mode != "quiz-immediate");
var dict = createCodeWellWithOptions(ex, x, y, width, height, question0Code, dropdownInfo, showFeedbackBool, showFeedbackBool, true, saveData);
dropdownList = dict["dropdown"];
question0CodeWell = dict["code"];
dropdownDefaults = ex.data.instance.state.dropdownDefaults;
//Save dropdown selected text, extract it from the html
var string1 = 'data-toggle="dropdown">';
var string2 = ' <span class="caret">';
for (var i = 0; i < dropdownDefaults.length; i++) {
dropdownList[i].dropdown.text(dropdownDefaults[i]);
if (wasSubmitButtonClicked) {
if (isCorrectAnswerBeingDisplayed) {
dropdownList[i].dropdown.style({color : "green"});
} else {
dropdownList[i].dropdown.style({color : oldColors[i]});
};
dropdownList[i].dropdown.disable();
};
};
};
break;
case 1:
buttonCode = ex.data.instance.state.buttonCode;
buttonInfo = ex.data.instance.state.buttonInfo;
buttonDict = ex.data.instance.state.buttonDict;
correctAnswers = ex.data.instance.state.correctAnswers;
q1OldAnswers = ex.data.instance.state.q1OldAnswers;
q1OldColors = ex.data.instance.state.q1OldColors;
var descr = ex.data.instance.state.descr;
pressedButtons = ex.data.instance.state.pressedButtons;
indexPressedButtons = ex.data.instance.state.indexPressedButtons;
buttonHeader = ex.createParagraph(0,0,descr,{size : "xlarge", width : ex.width(), textAlign : "center"});
var margin = 50;
var x = 50;
var y = buttonHeader.height() + margin;
var width = ex.width() - x - margin;
var height = ex.height() - y - margin;
var showFeedbackBool = (ex.data.meta.mode != "quiz-immediate");
var dict = clickableCodeWell(x, y, width, height, buttonCode, buttonInfo, buttonDict, showFeedbackBool, showFeedbackBool, true)
buttonList = dict["button"];
buttonCodeWell = dict["code"];
for (var i = 0; i < buttonList.length; i++) {
if (buttonList[i]["correct"] == true) {
buttonList[i]["button"].index = i;
buttonList[i]["button"].feedback = buttonList[i]["feedback"];
buttonList[i]["button"].on("click", function() {
var index = indexPressedButtons.indexOf(this.index);
if (index > -1) {
pressedButtons.splice(index,1);
indexPressedButtons.splice(index,1);
this.style({color : "white"});
}
else {
pressedButtons.push(true);
indexPressedButtons.push(this.index);
this.style({color : "blue"});
if (showFeedbackBool) showFeedback(this.feedback, true);
}
saveData();
});
}
else {
buttonList[i]["button"].index = i;
buttonList[i]["button"].feedback = buttonList[i]["feedback"];
buttonList[i]["button"].on("click", function() {
var index = indexPressedButtons.indexOf(this.index);
if (index > -1) {
pressedButtons.splice(index, 1);
indexPressedButtons.splice(index,1);
this.style({color : "white"});
}
else {
pressedButtons.push(false);
indexPressedButtons.push(this.index);
this.style({color : "blue"});
if (showFeedbackBool) showFeedback(this.feedback, false);
}
saveData();
});
}
}
break;
case 2:
var left = ex.data.instance.state.question2LeftCode;
var right = ex.data.instance.state.question2RightCode;
var q = ex.data.instance.state.question2Question;
var ca = ex.data.instance.state.question2Ca;
question2qtype = ex.data.instance.state.question2qtype;
currentIndentPrac = IndentPrac(left,right,q,ca);
currentIndentPrac.clicked = ex.data.instance.state.question2Clicked;
if (currentIndentPrac.clicked == 1){
currentIndentPrac.leftCard.clicked = true;
}
else if (currentIndentPrac.clicked == -1){
currentIndentPrac.rightCard.clicked = true;
}
currentIndentPrac.submitted = ex.data.instance.state.question2submitted;
};
return true;
};
return false;
};
/* Handles clicks to the submit button
*/
var submit = function () {
wasSubmitButtonClicked = true;
var buttonText = "Next";
if (questionNum == totalNumOfQs) buttonText = "End";
switch (questionType) {
case 0:
/* Generate Feedback */
var numOfCorrectDropdowns = 0;
if (dropdownList === undefined) {
throw "AMAL-DEFINED EXCEPTION: dropdownList is undefined";
};
for (var i = 0; i < dropdownList.length; i++) {
var currText = dropdownToDefaultLabel(dropdownList[i].dropdown);
console.log(currText);
console.log(dropdownList[i].correct);
if (currText == dropdownList[i].correct) {
console.log("got currect answer");
numOfCorrectDropdowns = numOfCorrectDropdowns + 1;
dropdownList[i].dropdown.style({color: "green"});
oldColors[i] = "green";
} else {
dropdownList[i].dropdown.style({color: "red"});
oldColors[i] = "red";
};
/* Disable dropdown */
dropdownList[i].dropdown.disable();
};
score = score + numOfCorrectDropdowns;
totalPossibleScore = totalPossibleScore + dropdownList.length;
var beginning = "";
if (numOfCorrectDropdowns == dropdownList.length) beginning = "Congratulations! "
//if (numOfCorrectDropdowns >= dropdownList.length/2) beginning = "Good job! "
var feedback = beginning.concat("You got ").concat(String(numOfCorrectDropdowns)).concat(" dropdowns correct out of ").concat(String(dropdownList.length)).concat(". Press '").concat(buttonText).concat("' to move on.");
showFeedback(feedback);
if (ex.data.meta.mode == "practice") ex.chromeElements.displayCAButton.enable();
break;
case 1:
console.log(pressedButtons);
var numOfCorrectButtons = 0;
var numOfWrongButtons = 0;
var total = 0;
if (buttonList == undefined) {
throw "buttonList is undefined!";
}
for (var j = 0; j < buttonList.length; j++) {
if (buttonList[j]["correct"] == true) {
total++;
buttonList[j]["button"].style({color : "green"});
q1OldColors[j] = "green";
}
else {
buttonList[j]["button"].style({color : "red"});
q1OldColors[j] = "red";
}
buttonList[j]["button"].disable();
}
for (var i = 0; i < pressedButtons.length; i++) {
console.log(pressedButtons);
if (pressedButtons[i] == true) {
numOfCorrectButtons++;
}
else {
numOfWrongButtons++;
}
}
pressedButtons = [];
score = score + numOfCorrectButtons - numOfWrongButtons;
totalPossibleScore = totalPossibleScore + total;
var beginning = "";
if (numOfCorrectButtons == total) beginning = "Congratulations! ";
var feedback = beginning.concat("You got ").concat(String(numOfCorrectButtons)).concat(" buttons correct out of ").concat(String(total)).concat(". Press 'Next' to move on.");
if (numOfWrongButtons > 0) feedback = feedback.concat(" You also pressed ").concat(String(numOfWrongButtons)).concat(" button(s) incorrectly.")
showFeedback(feedback);
if (ex.data.meta.mode == "practice") ex.chromeElements.displayCAButton.enable();
break;
case 2:
currentIndentPrac.submit();
currentIndentPrac.submitted = true;
currentIndentPrac.leftCard.well.off("click");
currentIndentPrac.rightCard.well.off("click");
saveData();
if (ex.data.meta.mode == "practice") ex.chromeElements.displayCAButton.enable();
break;
};
ex.chromeElements.submitButton.disable();
createNextButton(buttonText);
saveData();
};
var createNextButton = function(buttonText) {
/* Create the next button */
if (buttonText === undefined) {
var buttonText = "Next";
if (questionNum == totalNumOfQs) buttonText = "End";
};
var x = ex.width()-100;
var y = ex.height()-42;
nextButton = ex.createButton(x, y,buttonText, {
color: "white",
size: "medium"
});
nextButton.on("click", function () {
ex.chromeElements.submitButton.enable();
wasSubmitButtonClicked = false;
isCorrectAnswerBeingDisplayed = false;
if (feedbackAlert !== undefined) {
feedbackAlert.remove();
feedbackAlert = undefined;
isFeedbackShowing = false;
}
deleteAll();
//if (questionType == 2) {
// if (currentIndex+1 < type2List.length)currentIndex++;
// currentIndentPrac = type2List[currentIndex];
//}
questionType = (questionType+1)%3;
if (questionType == 2){
var vars = createCode(2,question2qtype);
currentIndentPrac = IndentPrac(vars.leftCode,vars.rightCode,
vars.question,vars.ca,vars.hint);
saveData();
}
if (ex.data.meta.mode == "quiz-immediate") {
questionNum++;
if (questionNum > totalNumOfQs) {
endOfExcercise();
console.log(endOfExercise);
nextButton.remove();
ex.chromeElements.submitButton.disable();
saveData();
return;
}
};
/* USE THIS SPACE TO CHANGE QUESTION TYPE OR TO CHECK IF YOU HAVE
* REACHED THE MAX NUMBER OF QUESTIONS!
*/
showQuestion();
nextButton.remove();
/* Disable Display CA Button */
ex.chromeElements.displayCAButton.disable();
saveData();
});
}
var showFeedback = function (feedback, correct) {
var color = "yellow";
if (correct === true) {
color = "green";
}
if (correct === false) {
color = "red";
}
if (feedbackAlert !== undefined) {
feedbackAlert.remove();
feedbackAlert = undefined;
isFeedbackShowing = false;
}
feedbackText = feedback;
isFeedbackShowing = true;
feedbackAlert = ex.alert(feedback, {
fontSize: 20,
stay: true,
color: color
});
}
/* End of excercise
*/
var endOfExcercise = function () {
endOfExercise = true;
var title = "End";
if (ex.chromeElements.titleHeader === undefined) ex.setTitle(title);
else ex.chromeElements.titleHeader.text(title);
var percent = score/totalPossibleScore;
var feedback = "You got ".concat(String(score)).concat(" out of ").concat(String(totalPossibleScore)).concat(" points.\n ").concat(String(percent*100)).concat("%");
ex.setGrade(percent, feedback);
showFeedback(feedback);
}
/* Delete element112s
*/
var deleteAll = function () {
switch (questionType) {
case 0:
if (header !== undefined) {
console.log("removed header");
header.remove();
header = undefined;
}
if (question0CodeWell !== undefined) {
console.log("removed question0CodeWell");
question0CodeWell.remove();
question0CodeWell = undefined;
}
if (dropdownList != undefined){
for (var i = 0; i < dropdownList.length; i++) {
console.log("removed dropdown ".concat(String(i)));
/* Remove dropdown */
dropdownList[i].dropdown.remove();
};}
dropdownList = undefined;
break;
case 1:
console.log("in button deleteall");
if (buttonHeader !== undefined) {
console.log("remove buttonHeader");
buttonHeader.remove();
buttonHeader = undefined;
}
if (buttonCodeWell !== undefined) {
console.log("removed buttonCodeWell");
buttonCodeWell.remove();
buttonCodeWell = undefined;
}
for (var i = 0; i < buttonList.length; i++) {
console.log("removed button ".concat(String(i)));
buttonList[i].button.remove();
}
buttonList = undefined;
break;
case 2:
currentIndentPrac.clear();
break;
};
};
/* Handles clicks to the displayCA button
*/
var displayCA = function () {
switch (questionType) {
case 0:
console.log(isCorrectAnswerBeingDisplayed);
if (isCorrectAnswerBeingDisplayed) {
for (var i = 0; i < dropdownList.length; i++) {
dropdownList[i].dropdown.text(oldAnswers[i]);
dropdownList[i].dropdown.style({color : oldColors[i]});
console.log(dropdownList[i].dropdown.text());
};
} else {
for (var i = 0; i < dropdownList.length; i++) {
oldAnswers[i] = dropdownToDefaultLabel(dropdownList[i].dropdown);
dropdownList[i].dropdown.text(dropdownList[i].correct);
dropdownList[i].dropdown.style({color : "green"});
console.log(dropdownList[i].dropdown.text());
};
};
break;
case 1:
if (isCorrectAnswerBeingDisplayed) {
for (var i = 0; i < q1OldAnswers.length; i++) {
buttonList[i]["button"].text(q1OldAnswers[i]);
console.log(q1OldColors[i]);
buttonList[i]["button"].style({color : q1OldColors[i]});
}
}
else {
for (var i = 0; i < buttonList.length; i++) {
q1OldAnswers[i] = buttonList[i]["button"].text();
buttonList[i]["button"].text(correctAnswers["_" + (i + 1).toString()]);
buttonList[i]["button"].style({color : "orange"});
}
}
break;
case 2:
currentIndentPrac.clear();
currentIndentPrac.draw();
break;
};
isCorrectAnswerBeingDisplayed = !isCorrectAnswerBeingDisplayed;
saveData();
};
/* Handles clicks to the reset button
*/
var reset = function () {
isCorrectAnswerBeingDisplayed = false;
enableButtons();
if (nextButton !== undefined) nextButton.remove();
if (feedbackAlert !== undefined) {
feedbackAlert.remove();
feedbackAlert = undefined;
isFeedbackShowing = false;
}
console.log("reset");
deleteAll();
if (questionType == 2){
var vars = createCode(2,question2qtype);
currentIndentPrac = IndentPrac(vars.leftCode,vars.rightCode,
vars.question,vars.ca,vars.hint);
saveData();
}
showQuestion();
console.log("showedQuestion");
// switch (questionType) {
// case 0:
// break;
// case 1:
// break;
// case 2:
// break;
// };
};
/* Turns binds button handlers
*/
var bindButtons = function () {
ex.chromeElements.submitButton.off("click");
ex.chromeElements.submitButton.on("click", submit);
ex.chromeElements.displayCAButton.off("click");
ex.chromeElements.displayCAButton.on("click", displayCA);
ex.chromeElements.resetButton.off("click");
ex.chromeElements.resetButton.on("click", reset);
ex.chromeElements.redoButton.off("click");
ex.chromeElements.undoButton.off("click");
};
/* Enables/disables buttons
*/
var enableButtons = function () {
if (ex.data.meta.mode == "practice") {
ex.chromeElements.submitButton.enable();
ex.chromeElements.displayCAButton.disable();
ex.chromeElements.undoButton.disable();
ex.chromeElements.redoButton.disable();
ex.chromeElements.resetButton.enable();
ex.chromeElements.newButton.enable();
};
if (ex.data.meta.mode == "quiz-immediate") {
ex.chromeElements.submitButton.enable();
ex.chromeElements.displayCAButton.disable();
ex.chromeElements.undoButton.disable();
ex.chromeElements.redoButton.disable();
ex.chromeElements.resetButton.disable();
ex.chromeElements.newButton.disable();
};
};
var run = function () {
bindButtons();
if (!(loadData())) {
enableButtons();
showQuestion();
};
if (endOfExercise == false && questionType == 2) showQuestion();
}
function CodeCard(left,content,ca,hint){
var code2 = {};
code2.ca = ca;
code2.hint = hint;
code2.margin = 10;
code2.left = left;
if (code2.left) code2.x = code2.margin;
else code2.x = code2.margin + ex.width()/2;
code2.y = 10;
code2.width = ex.width()/2 - code2.margin*2;
code2.height = ex.height()*2/3;
code2.content = content;
code2.well = undefined;
code2.clicked = false;
code2.draw = function(color){
//console.log(code2.width,code2.height);
code2.well = ex.createCode(code2.x,code2.y,code2.content,
{language:"python",
selectable:true,
border:"none",
width:code2.width,
height:code2.height
}).on("click",function(){
if (isCorrectAnswerBeingDisplayed) return;
code2.clicked = true;
code2.highlight();
console.log(code2.ca);
if (!code2.ca){
if (ex.data.meta.mode == "practice"){
ex.alert(hint, {
fontSize: 20,
stay: true,
color:"yellow"
});
}
}
if (code2.left) {
currentIndentPrac.clicked = 1;
currentIndentPrac.rightCard.clicked = false;
}
else {
currentIndentPrac.clicked = -1;
currentIndentPrac.leftCard.clicked = false;
}
saveData();
currentIndentPrac.redrawCard();}
);
ex.graphics.ctx.strokeStyle = "grey";
ex.graphics.ctx.strokeRect(code2.x,code2.y,code2.width,code2.height);
if (isCorrectAnswerBeingDisplayed){
if (code2.ca) code2.highlight("green");
else code2.highlight("red");
return;
}
if (code2.clicked) code2.highlight(color);
}
code2.clear = function(){
ex.graphics.ctx.clearRect(code2.x-code2.margin,code2.y-code2.margin,
ex.width()/2,code2.height+15);
if (code2.well != undefined){
code2.well.remove();
}
}
code2.highlight = function(color){
if (color == undefined) ex.graphics.ctx.strokeStyle = "blue";
else ex.graphics.ctx.strokeStyle = color;
ex.graphics.ctx.strokeRect(code2.x-4,code2.y-2,code2.width+6,code2.height+4);
}
return code2;
}
function IndentPrac(leftCode,rightCode,question,ca,hint){
var q = {}
var leftC = (ca == 1);
var rightC = (ca == -1);
q.leftCard = CodeCard(true,leftCode,leftC,hint);
q.rightCard = CodeCard(false,rightCode,rightC,hint);
if (ca == 1) q.cCard = q.leftCard;
else q.cCard = q.rightCard;
q.question = question;
q.ca = ca;
q.clicked = 0;
q.textPara = undefined;
q.x = 20;
q.y = ex.height()*3/4;
q.leftCode = leftCode;
q.rightCode = rightCode;
q.hint = hint;
q.submitted = false;
//q.explanation = explanation;
q.drawQuestion = function(){
q.textPara = ex.createParagraph(q.x,q.y,
"Click on the code that correctly "+q.question,
{size: "xlarge", width:ex.width()-100, textAlign:"center"});
}
q.draw = function(){
q.leftCard.draw();
q.rightCard.draw();
q.drawQuestion();
}
q.redrawCard = function(){
if (q.clicked == 1){
q.rightCard.clear();
q.rightCard.draw();
}
else if (q.clicked == -1){
q.leftCard.clear();
q.leftCard.draw();
}
}
q.clear = function(){
q.leftCard.clear();
q.rightCard.clear();
if (q.textPara != undefined){
q.textPara.remove();
}
}
q.submit = function(){
var text;
if (questionNum == totalNumOfQs) text = "Click on end to complete the quiz.";
else text = "Click on next to move on.";
if (!q.submitted){
totalPossibleScore += 4;
if (q.clicked == ca) score += 4;
q.submitted = true;
}
if (q.clicked == 0) {
if (ca == 1) q.leftCard.highlight("green");
else q.rightCard.highlight("green");
showFeedback("Incorrect! "+text);
return;
}
if (q.clicked == ca){
q.cCard.clear();
q.cCard.draw("green");
}
else {
q.cCard.highlight("green");
}
if (ex.data.meta.mode == "quiz-immediate"){
if (q.clicked == ca) showFeedback("Correct! "+text);
else showFeedback("Incorrect! "+text);
}
else{
if (q.clicked == ca) showFeedback("Correct! "+text);
else showFeedback("Incorrect! " +text );
}
}
return q;
}
var clickableCodeWell = function(x, y, width, height, code, buttonInfo, buttonDict, showFeedbackIfCorrect, showFeedbackIfWrong, randomDefault) {
console.log("clickableCodeWell");
console.log(ex.data.instance.state);
console.log(ex.data.instance.state.indexPressedButtons);
console.log(ex.data.instance.state.pressedButtons);
/* Create the code well */
buttonCodeWell = ex.createCode(x, y, code, {size:"large", width:width, height:height});