I need help to understand and practice the reduce() method in Js.
I want to calculate the total number of exercises in each course with the help of reduce(). \
Is this the correct way?
s = previousValue ,
p = currentValue, \
Update
as @Rani Sharim mentioned in the comment, I have to use reduce() on the parts array.
courses.map((course) => { console.log(course.parts.reduce((s, p) => s + p.exercises)) })
const courses = [
{
name: "Half Stack application development",
id: 1,
parts: [
{
name: "Fundamentals ",
exercises: 10,
id: 1,
},
{
name: "Using data",
exercises: 7,
id: 2,
},
{
name: "State ",
exercises: 14,
id: 3,
},
{
name: "React",
exercises: 11,
id: 4,
},
],
},
{
name: "Node.js",
id: 2,
parts: [
{
name: "Routing",
exercises: 3,
id: 1,
},
{
name: "Middlewares",
exercises: 7,
id: 2,
},
],
},
];