DEV Community

Discussion on: JavaScript Katas: Higher Version

Collapse
 
westim profile image
Tim West

I appreciate the desire for a single exit point, but seems unnecessary in this situation.

Here's a version that's much easier to read based on the CodeWars discussion with the additional feature of handling mismatched lengths:

function higherVersion(ver1, ver2) {
    ver1 = ver1.split('.').map(Number);
    ver2 = ver2.split('.').map(Number);
    const limit = Math.min(ver1.length, ver2.length);
    for (let i = 0; i < limit; i++)
        if (ver1[i] !== ver2[i])
            return ver1[i] > ver2[i];

    return ver1.length > ver2.length;  
}