Q:

Write a JavaScript program to count the occurrences of a value in an array

0

Write a JavaScript program to count the occurrences of a value in an array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 1));
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 2));
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 3));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now