Javascript Array numberOfOccurrences(s)

Description

Javascript Array numberOfOccurrences(s)


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

Array.prototype.numberOfOccurrences = function(s) {
  return this.filter(val => val === s).length;
}

console.log([0, 1, 2, 2, 3].numberOfOccurrences(0));
console.log([0, 1, 2, 2, 3].numberOfOccurrences(4));
console.log([0, 1, 2, 2, 3].numberOfOccurrences(2));
console.log([0, 1, 2, 2, 3].numberOfOccurrences('a'));



PreviousNext

Related