-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscramble.js
More file actions
executable file
·69 lines (65 loc) · 1.96 KB
/
scramble.js
File metadata and controls
executable file
·69 lines (65 loc) · 1.96 KB
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
function scrambledString(
tag,
objName,
initScrambledString,
initScrambledStringIndices
) {
this.tag = tag;
this.objName = objName;
this.string = initScrambledString;
this.indices = initScrambledStringIndices;
this.rescramble = rescramble;
this.initAnimatedBubbleSort = initAnimatedBubbleSort;
this.bubbleSortStep = bubbleSortStep;
this.bubbleSortBookmark = 0;
this.rescramble();
this.tag.innerHTML =
this.string +
' [ <a href="#" onClick="' +
this.objName +
'.initAnimatedBubbleSort();return false;">show</a> ]';
}
function rescramble() {
for (i = 0; i < this.indices.length; i++) {
indexToMove = Math.floor(Math.random() * (this.indices.length - i));
charIndexRemoved = this.indices.splice(indexToMove, 1);
this.indices = this.indices.concat(charIndexRemoved);
scrambledStringTemp =
this.string.substring(0, indexToMove) +
this.string.substring(indexToMove + 1) +
this.string.substring(indexToMove, indexToMove + 1);
this.string = scrambledStringTemp;
}
}
function initAnimatedBubbleSort() {
this.interval = setInterval(this.objName + ".bubbleSortStep()", 12);
}
function bubbleSortStep() {
if (this.bubbleSortBookmark >= this.indices.length - 1) {
this.bubbleSortBookmark = 0;
}
for (i = this.bubbleSortBookmark; i < this.indices.length - 1; i++) {
if (i == 0) {
this.changed = 0;
}
if (this.indices[i] > this.indices[i + 1]) {
this.changed = 1;
tempIndex = this.indices[i];
this.indices[i] = this.indices[i + 1];
this.indices[i + 1] = tempIndex;
tempArrange =
this.string.substring(0, i) +
this.string.substring(i + 1, i + 2) +
this.string.substring(i, i + 1) +
this.string.substring(i + 2);
this.string = tempArrange;
this.tag.innerHTML = this.string;
this.bubbleSortBookmark = i;
break;
}
}
this.bubbleSortBookmark = i;
if (!this.changed) {
clearInterval(this.interval);
}
}