-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsortchar.js
77 lines (64 loc) · 1.86 KB
/
sortchar.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
function SortModel() {
var self = this;
self.prefix = 'xyz';
Model.call(self);
self.title("Blocky Sort Characters");
self.reverse = ko.observable(false);
self.problem = ko.computed(function() {
if (self.reverse()) {
return "Define an algorithm in blockly that sorts the array below in descending order.";
} else {
return "Define an algorithm in blockly that sorts the array below in ascending order.";
}
}, self);
self.checkSucceeded = function() {
var valid = true;
var rev = self.reverse();
for (var i=1; i<self.array().length; i++) {
if ((!rev & self.array()[i-1] > self.array()[i]) ||
(rev & self.array()[i-1] < self.array()[i]) ) {
valid = false;
break;
}
};
self.succeeded(valid);
}
self.newProblem = function() {
self.seed(Math.ceil(Math.random() * 1000));
self.reset();
}
self.sortedProblem = function() {
self.seed(Math.ceil(Math.random() * 20) - 20);
self.reset();
}
self.invSortedProblem = function() {
self.seed(Math.ceil(Math.random() * 80) - 100);
self.reset();
}
self.reset = function() {
var seed = self.seed();
self.interpreter = null;
self.succeeded(false);
self.steps(0);
self.array.removeAll();
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var ar = [];
var rng = new MersenneTwister(seed);
for (var i=0; i<self.n(); i++) {
ar.push(possible.charAt(Math.floor(rng.random() * possible.length)));
}
if (seed < 0) {
if (seed > -20) {
ar.sort()
} else {
ar.sort(function(a,b) { b-a;});
}
}
self.array(ar);
}
self.newProblem();
}
SortModel.prototype = Object.create(Model.prototype);
SortModel.prototype.init = function() {
Object.getPrototypeOf(SortModel.prototype).init.call(this);
}