-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
39 lines (32 loc) · 990 Bytes
/
demo.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
var compareVersion = function (first, second) {
var versionToArray = function(str) {
var version = (str + '').split('.'),
length = version.length, i = 0;
for ( ; i < length; i++) {
version[i] = parseInt(version[i], 10);
}
return version;
}, length, i = 0, n1, n2;
first = versionToArray(first);
second = versionToArray(second);
// if arrays are equal - we have equal versions
if (first === second) {
return 0;
}
length = Math.max(first.length, second.length);
for ( ; i < length; i++) {
n1 = first[i] || 0;
n2 = second[i] || 0;
if (n1 === n2) {
continue;
}
return n1 - n2 > 0 ? 1 : -1;
}
}
$(function() {
// test code
$('button').on('click', function() {
var result = compareVersion($('#v1').val(), $('#v2').val());
$('span').text((result === 1) ? ' > ' : (result === -1) ? ' < ' : ' == ');
});
});