Nodejs Array Remove removeNegatives()

Here you can find the source of removeNegatives()

Method Source Code

// implement a funciton removeNegatives() that accepts an array and removes any values that removes any values that are less than zero. do this without two nested loops and do this in place. Can assume array only consists of numbers

// Added removeNegatives to prototype of the Array Object
Array.prototype.removeNegatives = function() {
   var negCount = 0;
   for (var index = 0; index < this.length; index++) {
      if(this[index] >= 0){
         this[index - negCount] = this[index];
      } else {/* www  .j  ava 2 s  .c  om*/
         negCount++
      }
   }
   this.length = this.length-negCount;
}

array2 = [-1,2.4,-3.1,4,-5.0,-6];
array2.removeNegatives();
console.log(array2);

Related

  1. removeElement()
    Array.prototype.removeElement = function()
        var what, a = arguments, L = a.length, ax;
        while(L && this.length){
            what= a[--L];
            while((ax= this.indexOf(what))!= -1){
                this.splice(ax, 1);
        return this;
    };
    
  2. removeElementByProperty(chave,obj)
    Array.prototype.removeElementByProperty = function(chave,obj) {
        var i = this.length;
        while (i--) {
          if (angular.equals(this[i][chave], obj)) {
              this.splice(i,1);
    };
    
  3. removeEmpty()
    Array.prototype.removeEmpty = function () {
      for (var i = 0; i < this.length; ++i) {
        if (this[i].replace(/^\s*$/, "") === "") {
          this.splice(i--, 1);
      return this;
    };
    
  4. removeEmpty()
    Array.prototype.removeEmpty = function() {
      var cleanArray = this.filter(function(n) {
          return (n !== undefined && n !== null && n !== '');
      });
      return cleanArray;
    };
    
  5. removeMultiple(values)
    Array.prototype.removeMultiple = function(values)
      for (var i = 0; i < values.length; i++)
        this.splice(this.indexOf(values[i]), 1);
        i--;
    
  6. removeRange(start, end)
    Array.prototype.removeRange = function(start, end){
      this.splice(start, end-start);
    
  7. removeSame()
    Array.prototype.removeSame = function() {
      let theNewArray = [];
      this.map((value, index, array) => {
        if (array.indexOf(value) === index) {
          theNewArray.push(value);
      });
      return theNewArray;
    };
    ...
    
  8. removeWhere(property, value)
    Array.prototype.removeWhere = function (property, value) {
        "use strict";
        for(var i = 0; i < this.length; i++) {
            if(this[i][property] === value) {
                this.splice(i, 1);
                break;
    };
    ...
    
  9. remove_(integer_list, value_list)
    Array.prototype.remove_ = function(integer_list, value_list) {
      return integer_list.filter( function(i) {
        return !value_list.some(function(v) {
          return v === i;
        });
      });
    var l = []
    var integer_list =  [1, 1, 2 ,3 ,1 ,2 ,3 ,4]
    ...