Nodejs Array Item Occurrence numberOfOccurrences(values)

Here you can find the source of numberOfOccurrences(values)

Method Source Code

/* numberOfOccurrences //from w w w .  j  ava2 s.  c  om

The numberOfOccurrences function must return the number of occurrences of an element in an array.

var arr = [0,1,2,2,3];
arr.numberOfOccurrences(0) === 1;
arr.numberOfOccurrences(4) === 0;
arr.numberOfOccurrences(2) === 2;
arr.numberOfOccurrences("a") === 0;


*/

Array.prototype.numberOfOccurrences = function(values) {

var tracker = 0;

    for(var i = 0; i < this.length; i++)
    {
        if (this[i]== values){

            tracker++;
        }
    }
    
    console.log(tracker);
};


var arr = [0,1,2,2,3];
arr.numberOfOccurrences(0);
arr.numberOfOccurrences(4);
arr.numberOfOccurrences(2);
arr.numberOfOccurrences("a");

Related

  1. numberOfOccurrences(search)
    Array.prototype.numberOfOccurrences = function(search) {
      return this.filter( function(num){ return search === num } ).length;
    
  2. numberOfOccurrences(target)
    Array.prototype.numberOfOccurrences = function(target) {
      for(var i =0;i<this.length;i++){
        var x = this.filter(function(b){return b === target}).length
      return x;
    
  3. numberOfOccurrences(val)
    Array.prototype.numberOfOccurrences = function(val) {
      var count = 0;
      for(var i = 0; i < this.length; i++){
        if(this[i] === val){
          count++;
      return count;
    
  4. numberOfOccurrences(val)
    Array.prototype.numberOfOccurrences = function(val) {
      var count = 0;
      for(var i=0;i<this.length;i++){
        if(val === this[i]){
          count += 1;
      return count;
    
  5. numberOfOccurrences(value)
    Array.prototype.numberOfOccurrences = function(value) {
      var count = 0;
      this.forEach(function(elem, index){
        if(elem === value){
          count++;
      });
      return count;
    };
    ...