Comparing version numbers contained in a text string I was trying to make something to compare two version numbers. The version numbers should consist of three parts (1.4.3). I also wanted to take into account if there is badly formatted data or if 1.4 is used instead of 1.4.0. Any bad data will be converted to a 0. I wanted to find something simple and fast. This is what I came up with:

version(a,b){

    // insure that there are enough decimal points
    a += ".0.0.0"
    b += ".0.0.0"

    //take only the first three elements
    a_arr = a.split(".",3)
    b_arr = b.split(".",3)


    a_int = a_arr.map(function (value) {
        val = parseInt(value, 10)
        return val ? val : 0
    })

    b_int = b_arr.map(function (value) {
        val = parseInt(value, 10)
        return val ? val : 0
    })

    console.log(a_int)
    console.log(b_int)

    status = "equal"
    i = 0

    do {
        if (a_int[i] &lt; b_int[i])
            status = "less"
        else if (a_int[i] &gt; b_int[i])
            status = "greater"
        i++
    }
    while (status == "equal" &amp;&amp; i &lt; 3)

    console.log("Status:" + status)

}


What are your thoughts? Is it a bit too hackish? What would be a better approach?
a_int = a_arr.map(function (value) {
    val = parseInt(value, 10)
    return val ? val : 0
})

b_int = b_arr.map(function (value) {
    val = parseInt(value, 10)
    return val ? val : 0
})


This part can be extracted into its own function.

// insure that there are enough decimal points
a += ".0.0.0"
b += ".0.0.0"


Instead of appending an arbitrary number of segments, try using array.map against an array of 3 zeroes instead. Then either copy the segment from a_int/b_int if present or retain the zero.

status = "equal"
i = 0

do {
    if (a_int[i] &lt; b_int[i])
        status = "less"
    else if (a_int[i] &gt; b_int[i])
        status = "greater"
    i++
}
while (status == "equal" &amp;&amp; i &lt; 3)


This whole operation can be done with a reduce. Carry an "equal" string as initial value. Then loop through the 3 items in the array, comparing each, and updating the carried value to "greater" or "less".

version(a,b){


We don't really know from this alone which one is the reference, and which one is being compared. If the function returned "greater", is a greater than b or is b greater than a. Name the function and the variables properly.

Lastly, you forgot to define variables with either a var, let or const. The problem with not doing so, particularly with browser JS, is that declaring variables without them will shoot the variables to the global scope, defining them there instead of within this function. That means your variable is open for clobbering or is clobbering other variables of the same name.

This is how I would have carried this one out:



// Used as filler value and for us to loop 3 times when using array methods.
const filler = [0, 0, 0];

// Splits, normalizes into 3 numbers, zeroes invalid values
function versionToArray(version){
  const versionArray = version.split('.');
  return filler.map((fill, index) =&gt; {
    const segment = parseInt(versionArray[index], 10);
    return Number.isNaN(segment) ? fill : segment;
  });
}

function compareVersion(subject, reference){
  const ref = versionToArray(reference);
  const sub = versionToArray(subject);

  // Static values into constants
  const EQUAL = 'equal';
  const GREATER = 'greater';
  const LESS = 'less';

  return filler.reduce((status, segment, index) =&gt; {
    // One nice thing about ternaries is that they can be written this way.
    // Acts like a switch statements that cascade, while looking like a
    // key-value pair of condition-value.
    return status !== EQUAL ? status  // A greater segment already decided
                : sub &gt; ref ? GREATER
                : sub &lt; ref ? LESS
                            : EQUAL;  // If not less or greater, well...
  }, EQUAL); // Assume equal unless proven otherwise
}

console.log(compareVersion('1', '2'));
console.log(compareVersion('1.2', '2'));
console.log(compareVersion('2.2', '2'));
console.log(compareVersion('2.2.2', '2'));
console.log(compareVersion('2.0.0', '2'));
console.log(compareVersion('4.0.0', '2'));
console.log(compareVersion('1.1.1.1', '2'));