Nodejs Array Count countCattle(kind)

Here you can find the source of countCattle(kind)

Method Source Code

Array.prototype.countCattle = function (kind) {
  var numKind = 0;

  for (var i = 0; i < this.length; i++) {
    if (this[i].type == kind) {
      numKind++/* w ww.jav a 2  s . c om*/
    }
  }

  return numKind
}

var canyonCows = [
  { name: "Bessie", type: 'cow', hadCalf: 'Burt'},
  { name: "Bessie", type: 'bull', hadCalf: 'Burt'},
  { name: "Bessie", type: 'cow', hadCalf: 'Burt'},
  { name: "Bessie", type: 'cow', hadCalf: 'Burt'},
  { name: "Bessie", type: 'calf', hadCalf: null},
]

console.log(canyonCows.countCattle('cow'))

Related

  1. count(value)
    Array.prototype.count = function (value) {
      var count = 0, i;
      for (i = 0; i < this.length; ++i) {
        if (this[i] === value) {
          ++count;
      return count;
    };
    ...
    
  2. countCattle( kind )
    Array.prototype.countCattle = function ( kind ){
      var numKind = 0;
      for(var i = 0; i<this.length; i++){
        if(this[i].type == kind){
          numKind++;
      return numKind;
    };
    ...
    
  3. countCattle(cattle)
    Array.prototype.countCattle = function(cattle){
      var number = 0;
      for(var i = 0; i < this.length; i ++){
        if(this[i].type == cattle){
          number ++ ;
      return number;
    };
    ...
    
  4. countCattle(kind)
    Array.prototype.countCattle = function(kind) {
      var numKind = 0;
      for (var i = 0; i<this.length; i++) {
        if (this.type == kind){
          numKind++;
      return numKind;
    };
    ...
    
  5. countCattle(kind)
    Array.prototype.countCattle = function(kind) {
      var numKind = 0;
      for (var i = 0; i < this.length; i++) {
        if (this[i].type == kind) {
          numKind++;
      return numKind;
    };
    ...
    
  6. countCattle(kind)
    Array.prototype.countCattle = function(kind) {
      var numKind = 0;
      for (i in this) {
        if (this[i]["type"] == kind) numKind++;
      return numKind;
    };
    
  7. calculateCount()
    Array.prototype.calculateCount = function() {
        return this.length;
    };
    var a = ["1", "2"];
    var count = a.calculateCount();
    console.log(count);
    
  8. countForBreeding()
    Array.prototype.countForBreeding = function() {
      var numToBreed = 0;
      for (var i = 0; i < this.length; i++) {
        if (this[i].noCalvesYet()) {
          numToBreed++;
      return numToBreed;
    };
    ...
    
  9. countItem(item)
    Array.prototype.countItem = function (item) {
            return this.filter(function (i) {
                return i === item
            }).length;
    };