Need help with task

Hello so this was the task on codewars that I was solving today:

You will be given an array of objects (associative arrays in PHP) representing data about developers who have signed up to attend the next coding meetup that you are organising.

Your task is to return an object (associative array in PHP) which includes the count of each coding language represented at the meetup .

For example, given the following input array:

var list1 = [
  { firstName: 'Noah', lastName: 'M.', country: 'Switzerland', continent: 'Europe', age: 19, language: 'C' },
  { firstName: 'Anna', lastName: 'R.', country: 'Liechtenstein', continent: 'Europe', age: 52, language: 'JavaScript' },
  { firstName: 'Ramon', lastName: 'R.', country: 'Paraguay', continent: 'Americas', age: 29, language: 'Ruby' },
  { firstName: 'George', lastName: 'B.', country: 'England', continent: 'Europe', age: 81, language: 'C' },
];

your function should return the following object:

{ C: 2, JavaScript: 1, Ruby: 1 }

Notes:

  • The order of the languages in the object does not matter.
  • The count value should be a valid number.
  • The input array will always be valid and formatted as in the example above.

///////////////

So here is my solution:

function countLanguages(list) {
  
  let count = {};
  list.forEach(i => count[i.language] = (count[i.language] || 0) + 1);
  return count;

}

It all seems very compact and simple but I can’t figure out the callback function and forEach method in general. So can someone explain this to me?

I don’t understand. This is your solution? And you don’t understand the two most important components?

The forEach method is just a way of looping through an array. It is easily replaceable with a for loop. And the callback is just saying that the new count for that language … if it is currently undefined (or technically falsy), then assume that it’s 0 and add 1 to it. If it’s not 0, then go ahead and use that number and add 1 to it.

Really, in order to understand this, you need to understand the forEach method and you have to understand how the short-circuit OR works.

This is basically the same thing rewritten:

function countLanguages(list) {
  let count = {};
  for (let x = 0; x < list.length; x++) {
    let currentCount = count[list[x].language] === undefined ? 0 : count[list[x].language];
    // let currentCount = count[i.language] || 0;
    count[list[x].language] = currentCount + 1;
  }
  return count;
}

It is algorithmically the same, but is just stretched out a bit.

1 Like

Of course, that is nice and short. There’s also a one line solution.

Hint:

It involves the reduce method.

1 Like

Thank You but I still don’t understand how we managed to save a language’s name inside of our count variable. I get how we got the number of them that part is now clear.

This. count is the object. We are using bracket notation to access a prop on it. If i.language is “Pascal”, then we will be looking for the property count.Pascal. if it doesn’t already exist, this will create it, because we assign a value to it.

1 Like

Now I get it, thanks a lot!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.