Skip to content

Commit b6b5af0

Browse files
authored
Create 165-compare-version-numbers.js
1 parent 8d9d9ed commit b6b5af0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

165-compare-version-numbers.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} version1
3+
* @param {string} version2
4+
* @return {number}
5+
*/
6+
const compareVersion = function(version1, version2) {
7+
const fa = version1.split(".");
8+
const sa = version2.split(".");
9+
const len = Math.max(fa.length, sa.length);
10+
if (fa.length < len) {
11+
while (fa.length < len) {
12+
fa.push("0");
13+
}
14+
}
15+
if (sa.length < len) {
16+
while (sa.length < len) {
17+
sa.push("0");
18+
}
19+
}
20+
while (sa.length > 0 && fa.length > 0) {
21+
let fe = +fa.shift();
22+
let se = +sa.shift();
23+
if (fe > se) {
24+
return 1;
25+
}
26+
if (fe < se) {
27+
return -1;
28+
}
29+
}
30+
return 0;
31+
};

0 commit comments

Comments
 (0)