MCPcopy Create free account
hub / github.com/course-dasheng/fe-algorithm / compareVersion

Function compareVersion

interview/165.compare-version.js:7–21  ·  view source on GitHub ↗
(version1, version2)

Source from the content-addressed store, hash-verified

5 * rip https://twitter.com/haoel/status/1654331337971732480
6 */
7var compareVersion = function(version1, version2) {
8 const arr1 = version1.split('.')
9 const arr2 = version2.split('.')
10 const len = Math.max(arr1.length, arr2.length)
11 for(let i=0;i<len;i++){
12 const v1 = Number(arr1[i]) || 0
13 const v2 = Number(arr2[i]) || 0
14 if(v1<v2){
15 return -1
16 }else if(v1>v2){
17 return 1
18 }
19 }
20 return 0
21};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected