Maximum subarray problem in JavaScript I am new to JavaScript. I've recently completed first 10 Chapters of "JavaScript: The Good Parts". 

I've coded this small program to find maximum subarray of an array and would appreciate your feedback on the structure of the program (from JavaScript's point of view). I am basically looking for suggestions in following areas:


Objects being returned
Usage of Infinity
Usage of non-pure functions
Closures


Here is the code:

// Max. subarray from CLRS
var maxsubarr = function (arr) {
    var that  = {};

    var max_cross_subarr = function (low, mid, high) {
        var left_sum = -Infinity;
        var right_sum = -Infinity;
        var max_low;
        var max_high;

        var sum = 0;
        var i;
        for (i=mid; i &gt;=low; i--) {
            sum = sum + arr[i];
            if (sum &gt; left_sum) {
                left_sum = sum;
                max_low = i;
            }
        }
        sum = 0;
        for (i=mid+1; i &lt;=high; i++) {
            sum = sum + arr[i];
            if (sum &gt; right_sum) {
                right_sum = sum;
                max_high = i;
            }
        }

        return {
            low: max_low,
            high: max_high,
            sum: left_sum + right_sum
        };
    }   

    var max_sub_arr = function (low, high) {
        var leftarr, rightarr, mid;
        if (low === high) {
            return {
                low: low,
                high: high,
                sum: arr[low]
            };
        }
        else {
            mid = Math.floor((high+low)/2);
            leftarr = max_sub_arr (low, mid);
            rightarr = max_sub_arr (mid+1, high);
            crossarr = max_cross_subarr (low, mid, high);

            if (leftarr.sum &gt;= rightarr.sum &amp;&amp; leftarr.sum &gt;= crossarr.sum) {
                return leftarr;
            } else if (rightarr.sum &gt;= leftarr.sum &amp;&amp; rightarr.sum &gt;= crossarr.sum) {
                return rightarr;
            } else {
                return crossarr;
            }
        }
    }

    that.find = function () {
        return max_sub_arr (0, arr.length - 1);
    }

    return that;
}

var mb = maxsubarr ([13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]);
console.log(mb.find());
Objects being returned.
  


This is fine as long as you define a convention and stick to it.


  
  Usage of Infinity
  


Your use case looks fine, you're using it as an upper and lower bound.


  
  Usage of non-pure functions
  


This one is a little subjective. For an algorithm like this, it makes sense to only write pure functions as that can make debugging easier. Your code is readable and clear, if a little contrived, but I wouldn't worry too much about this. JavaScript is sort of a weird language to ask this about since most functions won't follow this rule especially when you do DOM manipulation and frontend JavaScript.


  
  Closures
  


Very common in JavaScript. You should explore node.js if you're interested in learning more.

Nit: var that = {};. Aside from being a completely uninformative and useless variable name, this declaration isn't needed. You can rewrite the return statement as:

return {
    find: function() {
        return max_sub_arr(0, arr.length - 1);
    }
};


Or even better:

return max_sub_arr(0, arr.length - 1);


I don't see why you need to return a function to be invoked.