Last active
January 2, 2018 00:52
-
-
Save ChildishGiant/1695c94adffaa3efe5e8f96c335ba822 to your computer and use it in GitHub Desktop.
For comparing semantic versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compareVersions(v1, v2) { // Returns the larger of the two. or "equal" | |
v1Array = v1.split("."); | |
v2Array = v2.split("."); | |
for(var i=0; i<v1Array.length;i++){v1Array[i] = parseInt(v1Array[i])}; | |
for(var i=0; i<v2Array.length;i++){v2Array[i] = parseInt(v2Array[i])}; | |
if (v1Array.length != 3 || v2Array.length != 3){ | |
return "Both versions need to be x.y.z"; | |
} else { | |
if (v1Array[0]>v2Array[0]){ | |
return v1; | |
} else if (v1Array[0]<v2Array[0]) { | |
return v2; | |
} else { | |
if (v1Array[1]>v2Array[1]){ | |
return v1; | |
} else if (v1Array[1]<v2Array[1]) { | |
return v2; | |
} else { | |
if (v1Array[2]>v2Array[2]){ | |
return v1; | |
} else if (v1Array[2]<v2Array[2]) { | |
return v2; | |
} else { | |
return "equal"; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment