I need help understanding methods

Tell us what’s happening:
I don’t understand how this reduce works. It’s really confusing to me because in the example given below, we are using the reduce method, and having arguments of a function with arguments of sum and user??? Then we define the function, saying to return sum + user.age??? I’m really confused because we never even defined the variable user, so how can we even provide a value for user.age? How does this function know that the user is even an object/array?

Your code so far
function getRating(watchList){

// Only change code below this line

var averageRating;

const users = [

{ name: ‘John’, age: 34 },

{ name: ‘Amy’, age: 20 },

{ name: ‘camperCat’, age: 10 }


const sumOfAges = users.reduce((sum, user) => sum + user.age, 0);

console.log(sumOfAges); // 64

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below, 
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

The reduce method takes 4 arguments. The order of the arguments is important. The first argument is the accumulator described in the challenge. The second argument is the element currently being iterated over. In this case, the elements of the users array are objects, which is why you can refer to an age property on user.

So, in this case, the arguments are sum and user, correct? I get that the accumulator is just named sum, but then if we are iterating over an object, shouldn’t we be iterating over the users object ? In this case, user has never been defined as an object being equal to users so therefore, user.age doesn’t make sense to me. It would make more sense to me if it was users.age instead. But why does it work to use user.age instead of users.age?

As already explained, user is each of the objects inside the users array. The object is being passed as an argument to the user parameter (a parameter is just a local variable, you can name it whatever you like).

const users = [
  { name: 'John', age: 34 },
  { name: 'Amy', age: 20 },
  { name: 'camperCat', age: 10 },

const sumOfAges = users.reduce((sum, user) => {
  console.log(user); // { name: 'John', age: 34 }, { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 }
  console.log(user.name); // John, Amy, camperCat
  console.log(user.age); // 34, 20, 10
}, 0);
1 Like

Can you help explain why the function has to be defined as (sum + user.age, 0)? What is the function of the 0 at the end of the function?

the 0 after the function is the second argument for the reduce method and it says the starting value of the first parameter of the callback function (if you don’t set it the starting value is the first element of the array)

try to play with this method, it can do a lot of things and you can feed it any array
try to look at the documentation