forked from cripi-javascript/dz-1-introduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdz1.js
32 lines (28 loc) · 978 Bytes
/
dz1.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
/*jslint browser: true*/
var arrayForSort = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function sRand() {
'use strict';
return Math.random() > 0.5 ? 1 : -1;
}
function sBubble(arrayForSort) {
'use strict';
var i, j, tmp;
for (i = 0; i < arrayForSort.length - 1; i += 1) {
for (j = 0; j < arrayForSort.length - 1; j += 1) {
if (arrayForSort[j + 1] < arrayForSort[j]) {
tmp = arrayForSort[j + 1];
arrayForSort[j + 1] = arrayForSort[j];
arrayForSort[j] = tmp;
}
}
}
return arrayForSort;
}
arrayForSort.sort(sRand);
window.addEventListener('load', function () {
'use strict';
document.getElementById('arrayForSort').innerHTML = arrayForSort;
document.getElementById('sortButton').addEventListener('click', function () {
document.getElementById('sortResult').innerHTML = sBubble(arrayForSort);
}, false);
}, false);