I have an array and need to count the number of occurrences of 3 values in it.
BOWL = [“jelly-bean”, “m&m”, “m&m”,“chocolate”, “m&m”, “jelly-bean”, “m&m”, “m&m”, “jelly-bean”];
How do I write the function that will tell me the numbers of occurrences of all 3 values?
I managed to do so only for 1 value but do not know how to do it for 3 values at the same time…
var BOWL = ["jelly-bean", "m&m", "m&m",
"chocolate", "m&m", "jelly-bean", "m&m", "m&m", "jelly-bean"];
var jbOccur = 0; //for "jelly-bean"
var mOccur = 0; //for "m&m"
var chocOccur = 0; //for "chocolate"
jbOccur = Occurrence(BOWL); //calling the function, sending the BOWL array
console.log (jbOccur);
//Define the function
function Occurrence(arr){
var CountjbOccur=0
for(var i=0;i<arr.length;i++){
if(arr[i]=="jelly-bean"){
CountjbOccur++;
}
}
return CountjbOccur
}