-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (54 loc) · 1.27 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
$(function(){
var stickHeight = []
var maxSticks;
var i = 1;
var didSort = false;
function fillStickHeight () {
var arr = [];
for (var x = 0; x < 50; x++){
stickHeight.push(Math.floor(Math.random() * 50 + 20));
}
maxSticks = stickHeight.length;
}
//initiate sticks
function createSticks() {
for (var n = 0; n < stickHeight.length; n++ ) {
$("#sort-area").append("<div class='stick' data-height=" + stickHeight[n] + " style='height:" + stickHeight[n] + "px'></div>");
}
}
function insertSticks() {
$("#sort-area").empty();
createSticks();
}
function goSort() {
var beforeStick = stickHeight[i - 1];
var afterStick = stickHeight[i];
if (beforeStick > afterStick) {
var afterTemp = afterStick;
stickHeight[i] = beforeStick;
stickHeight[i - 1] = afterTemp;
didSort = true;
insertSticks();
}
if (i === maxSticks - 1 && didSort === false){
alert('done!');
}
else {
startSort();
}
if (i === maxSticks - 1){
i = 1;
maxSticks--;
didSort = false;
}
else {
i++;
}
}
function startSort(){
window.setTimeout(goSort, 10);
}
fillStickHeight();
createSticks();
$('.sort-button').on('click', startSort);
});