const championObj = {1: "Annie", 2: "Olaf", 3: "Galio", 4: "Fizz"}
const championList = ['1','2','1','3','4','3','1']
function countChampions(championObj, championList) {
//create an empty object to store values
let obj = {}
//loop over championList array to compare with championObj object
for(var i = 0; i < championList.length; i++) {
//if the index is not in the object, add it into empty object and make the value 1
if(obj[championObj[championList[i]]] === undefined) {
obj[championObj[championList[i]]] = 1
//else if the index is already found in the object, increment the value by + 1
} else if (obj[championObj[championList[i]]] !== undefined) {
obj[championObj[championList[i]]] += 1
}
//return the object
return obj
}
}
console.log(countChampions(championObj, championList))
When I run this function, I only get { Annie: 1 }
The output should be = {"Annie" : 3, "Olaf" : 1, "Galio": 2, "Fizz": 1}