Comparing version numbers with JavaScript I have written a function that compares two version number strings (e.g. 1.2.0 and 1.2.2) and return 1 if the first string is greater, -1 if the second string is greater and 0 if the strings are equal for a code challenge that I'm attempting.
Also, it's guaranteed that both strings contain an equal number of numeric fields and all decimals are non-negative.
So,

1.2.0 and 1.2.2 should return -1.
1.05.4 and 1.5.3 should return 1.
1.2.6.76 and 1.2.06.0076 should return 0.


My logic for the function was to simply split the string at each occurrence of the period ., compare each number and then add a character (e for equal, m for more and l for less) to a temporary variable z depending on the comparison result.
I then simply use some basic regex to return 1, -1 or 0 based on the content of the z variable as can be seen in the following Code Snippet:


function checkVersion(a,b) {
    let x=a.split('.').map(e=&gt; parseInt(e));
    let y=b.split('.').map(e=&gt; parseInt(e));
    let z = "";

    for(i=0;i&lt;x.length;i++) {
        if(x[i] === y[i]) {
            z+="e";
        } else
        if(x[i] &gt; y[i]) {
            z+="m";
        } else {
            z+="l";
        }
    }
    if (!z.match(/[l|m]/g)) {
      return 0;
    } else if (!z.match(/[l]/g)) {
      return 1;
    } else {
      return -1;
    }
}

console.log(checkVersion("1.2.2","1.2.0")); // returns 1 as expected
console.log(checkVersion("1.0.5","1.1.0")); // returns -1 as expected
console.log(checkVersion("1.0.5","1.00.05")); // returns 0 as expected
console.log(checkVersion("0.9.9.9.9.9.9","1.0.0.0.0.0.0")) // returns -1 as expected;



The above function seems to be working fine with any random two version numbers that I've tried so far but when I try to submit the above function for the challenge, there is always one hidden test with two unknown version numbers that keeps failing. What logic am I missing in the above code?

EDIT:
Thanks to @RomanPerekhrest's comment below, I have found out that my regex is the problem. Instead of using the 2nd regex, I just remove any occurence of e from the z variable using the split() method and then just check if the first character is m or l and now the function is working correctly as seen in the following Code Snippet:


function checkVersion(a,b) {
    let x=a.split('.').map(e=&gt; parseInt(e));
    let y=b.split('.').map(e=&gt; parseInt(e));
    let z = "";

    for(i=0;i&lt;x.length;i++) {
        if(x[i] === y[i]) {
            z+="e";
        } else
        if(x[i] &gt; y[i]) {
            z+="m";
        } else {
            z+="l";
        }
    }
    if (!z.match(/[l|m]/g)) {
      return 0;
    } else if (z.split('e').join('')[0] == "m") {
      return 1;
    } else {
      return -1;
    }
}

console.log(checkVersion("2.0.5","1.0.15")); // returns 1 as expected
console.log(checkVersion("1.2.2","1.2.0")); // returns 1 as expected
console.log(checkVersion("1.0.5","1.1.0")); // returns -1 as expected
console.log(checkVersion("1.0.5","1.00.05")); // returns 0 as expected
console.log(checkVersion("0.9.9.9.9.9.9","1.0.0.0.0.0.0")) // returns -1 as expected;




However, I still feel like there must be a shorter, more concise and cleaner way of doing this though. Any suggestions?
A small review;


Once you know that one version digit is larger than the other, you can exit immediately
You did not declare i with const or let
I would advise the use of a beautifier for your code, it's a bit compact in some places
The code does not handle well versions with different counts of digits
You should always pass the base, when you call parseInt


I wrote an alternative version with 2 extra tests;



function checkVersion(a, b) {
    const x = a.split('.').map(e =&gt; parseInt(e, 10));
    const y = b.split('.').map(e =&gt; parseInt(e, 10));

    for (const i in x) {
        y[i] = y[i] || 0;
        if (x[i] === y[i]) {
            continue;
        } else if (x[i] &gt; y[i]) {
            return 1;
        } else {
            return -1;
        }
    }
    return y.length &gt; x.length ? -1 : 0;
}

console.log(checkVersion("1.2.2", "1.2.0"), 1); // returns 1 as expected
console.log(checkVersion("1.0.5", "1.1.0"), -1); // returns -1 as expected
console.log(checkVersion("1.0.5", "1.00.05"), 0); // returns 0 as expected
console.log(checkVersion("0.9.9.9.9.9.9", "1.0.0.0.0.0.0"), -1) // returns -1 as expected;
console.log(checkVersion("1.0.5", "1.0"), 1); // returns 1 as expected
console.log(checkVersion("1.0", "1.0.5"), -1); // returns -1 as expected
console.log(checkVersion('2019.09', '2019.9'), 0) // returns 0