0
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}

1
  • 1
    Premature return Commented Aug 28, 2020 at 0:17

1 Answer 1

4

You get only 1 result because you return obj in the loop, so at the first iteration you end the function. The solution is to return obj after the for block

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))

In addition, since you check your if twice for no reason and use only the values of the array and not the indexes themselves, I want so suggest improvements to your code:

const championObj = {
  1: "Annie",
  2: "Olaf",
  3: "Galio",
  4: "Fizz"
}
const championList = ['1', '2', '1', '3', '4', '3', '1']

function countChampions(championObj, championList) {
  let obj = {}

  for (let value of championList) {
    if (obj[championObj[value]]) {
      obj[championObj[value]]++;
    } else {
      obj[championObj[value]] = 1;
    }
  }

  return obj
}

console.log(countChampions(championObj, championList))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.