Count the occurrences of each element inside an array using reduce. My code was wrong!
My code:
function countOccurrences(arr) {
return arr.reduce(function(a, b){
var count = 0;
for(var i = 0; i < arr.length; i++){
if (arr[i] == b) {
return count + 1;
}
}
}, '');
}
console.log(countOccurrences(['a', 'b', 'c', 'b', 'a']));
Expect:
// {
// a: 2,
// b: 2,
// c: 1
// }
Thanks so much!