Hi @columbuschidozie1 !
I would suggest taking a closer look at the sample fcc gave you.
It will give you the basic structure on how to solve this challenge.
In the sample, we have an array called users
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
The reduce method takes in a callback function and an optional initial value.
array.reduce(callback, optional initial value)
In the sample, we are using an initial value of zero and this callback function.
(sum, user) => sum + user.age, 0
The sum
represents the accumulator or previous value from our callback function.
The user represents the current array value.
Basically what our code is doing is going through the users array and adding each of the ages and returning a single value.
In this case that return value would be 64.
You can run this code in your editor to see what values are outputted for each call of the callback function.
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const sumOfAges = users.reduce((sum, user) => {
console.log(`The current value of the accumulator(${sum}) + current user age(${user.age}) = ${sum + user.age}\n The accumulator is now ${sum + user.age}\n`)
return sum + user.age
}, 0);
Reduce is a tricky topic to understand at first.
It will probably take a few more articles, videos and practice before it makes total sense.
But for now, just heavily study the sample given to you and model your code after that for the challenge.
Hope that helps!