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
and1.2.2
should return -1.1.05.4
and1.5.3
should return 1.1.2.6.76
and1.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=> parseInt(e));
let y=b.split('.').map(e=> parseInt(e));
let z = "";
for(i=0;i<x.length;i++) {
if(x[i] === y[i]) {
z+="e";
} else
if(x[i] > 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=> parseInt(e));
let y=b.split('.').map(e=> parseInt(e));
let z = "";
for(i=0;i<x.length;i++) {
if(x[i] === y[i]) {
z+="e";
} else
if(x[i] > 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?
console.log(checkVersion("2.0.5","1.0.15"));
- it'll give-1
while the expected result is1
\$\endgroup\$e
from thez
variable and then checking the first letter. However, I still feel like the above function could be further improved though. Any suggestions? \$\endgroup\$