CompareVersion on Tabzilla (JavaScript)


  • 1
    T

    I just found a compareVersion function on Tabzilla.

    /* https://mozorg.cdn.mozilla.net/en-US/tabzilla/tabzilla.js */
    Tabzilla.compareVersion = function (a, b) {
        var num1, num2;
        a = ('' + a).split('.');
        b = ('' + b).split('.');
        while (a.length < b.length) { a.push('0'); }
        while (b.length < a.length) { b.push('0'); }
        for (var i = 0; i < a.length; i++) {
            num1 = parseInt(a[i], 10);
            num2 = parseInt(b[i], 10);
            if (num1 > num2) { return 1; }
            if (num1 < num2) { return -1; }
        }
        return 0;
    };
    

    And it can pass this question.

    The code is clean so i shared it to here. Hope it is helpful.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.