Nodejs Array Check isAllNegative()

Here you can find the source of isAllNegative()

Method Source Code

/*//from   ww w .j  a  v a  2 s  . co  m
   Find the sum of contiguous subarray within a one-dimensional array of numbers
   which has the largest sum.
*/

function maxSubArray (array) {
   var maxSoFar = 0,
         maxSum = 0;

   if (array.isAllNegative()) {
      var maxNum = Number.NEGATIVE_INFINITY;
      for (var j = 0, jLen = array.length; j < jLen; j++) {
         if (array[j] > maxNum) {
            maxNum = array[j]
         }
      }
      return maxNum;
   }

   for (var i = 0, len = array.length; i < len; i++) {
      maxSoFar += array[i];
      if (maxSoFar < 0) {
         maxSoFar = 0;
      }
      else if (maxSoFar > maxSum) {
         maxSum = maxSoFar;
      }
   }
   return maxSum;
}

Array.prototype.isAllNegative = function () {
   for (var i = 0, len = this.length; i < len; i++) {
      if (this[i] >= 0) {
         return false;
      }
   }
   return true;
}

var myArray = [-100,-2,-3,-4,-105,-104];
maxSubArray(myArray);

Related

  1. is(that)
    Array.prototype.is = function(that) {
      var i, len, ok = true;
      for (i = 0, len = this.length; i < len && ok; i++) {
        if (this[i] instanceof Array && that[i] instanceof Array) {
          ok = this[i].is(that[i]);
        } else {
          ok = this[i] === that[i];
      return ok;
    };
    
  2. isArray(obj)
    Array.isArray = Array.isArray || function (obj) {
      return {}.call(obj) === '[object Array]';
    };
    
  3. isBlank()
    function proxy(func, context, args) {
        return function () {
            return func.apply(context, args);
    Array.prototype.isBlank = function () {
        return !this.length;
    };
    
  4. isCharPresent(charSearch)
    Array.prototype.isCharPresent = function(charSearch) {
      var arr = this;
      var result = [];
      var testingStr = '';
      if (charSearch.length > 1) {
        return "You're searching for more than one char";
      for (var i = 0; i < this.length; i++) {
        if (arr[i].indexOf(charSearch) === -1) {
    ...
    
  5. isDefined(key)
    Array.prototype.isDefined = function (key) {
        return this.filter(c => {
            if (key) return c[key];
            return !!c;
        });