-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtryProblemPermutations.js
89 lines (71 loc) · 2.53 KB
/
tryProblemPermutations.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
/*
* Script to try every permutation of a answers to a quiz for self-study CS144 course (problem 2-2A)
* See https://lagunita.stanford.edu/courses/Engineering/Networking-SP/SelfPaced/courseware
*
* Run this script in chrome developer console.
*/
// HELPER FUNCTIONS
/*
* Return an array of all pessible permutations of binary arrays of length "size"
* For instance, for size = 2, returns [[0,0],[0,1],[1,0],[1,1]]
*/
function getPermutations(size) {
if (size == 1) {
return [[1], [0]];
}
var result = [];
var permsOfSizeMinusOne = getPermutations(size - 1);
for (var i = 0; i < permsOfSizeMinusOne.length; i++) {
var permutation = permsOfSizeMinusOne[i];
var permutationCopy = permutation.slice(0);
permutation.push(1);
permutationCopy.push(0);
result.push(permutation);
result.push(permutationCopy);
}
return result;
}
/* Click all check checkboxes corresponding to 1's in the given 'permutation' (an array of 1's and 0's) */
function clickBoxes(permutation) {
console.log("Clicking " + permutation)
for (var i = 0; i < permutation.length; i++) {
if (permutation[i] == 1) {
$("#input_i4x-Engineering-Networking-SP-problem-c32e8e8839bb4910bfb74ddea1a340ed_2_1_choice_" + i).click();
}
}
}
/* Look at the answer object to see if the last answer (assumed to be 'permutation') was correct, alert if so */
function checkAnswer(permutation) {
console.log("Checking answer")
var statusObj = $("#status_i4x-Engineering-Networking-SP-problem-c32e8e8839bb4910bfb74ddea1a340ed_2_1")
if (statusObj.attr('class') == "status correct") {
alert("BOOMSLAM it's " + permutation);
console.log("It was " + permutation);
}
}
/* Click the "submit" button */
var submitFn = function() {
$("button.Submit")[4].click();
};
// SCRIPT CONTENTS
var permutations = getPermutations(7);
// Go through each permutation, click the corresponding boxes, submit the answer,
// and wait to see if it was correct. Make sure to wait a while for the server to respond
for (var i = 0; i < permutations.length; i++) {
var permutation = permutations[i];
var clickFn = (function(perm) {
return function() {
clickBoxes(perm);
}
})(permutation);
var checkAnswerFn = (function(perm) {
return function() {
checkAnswer(perm);
}
})(permutation);
var WAIT_TIME = 3000;
setTimeout(clickFn, WAIT_TIME * i);
setTimeout(submitFn, WAIT_TIME * i + WAIT_TIME / 10);
setTimeout(checkAnswerFn, WAIT_TIME * i + WAIT_TIME * (8 / 10));
setTimeout(clickFn, WAIT_TIME * i + WAIT_TIME * (9 / 10));
}