-3


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,
        },
      ],
    },
  ];
Kim-Jun-Un
  • 83
  • 2
  • 11
  • No. Parts is the array, not exercises. You need something like course.parts.reduce((part, current) => part. exercises + current) – Rani Sharim Oct 10 '21 at 19:34
  • @RaniSharim it will not work since part and current both are objects, to avoid such problem, better initialize first argument which is `previousValue` - I call it accumulator, with 0 – num8er Oct 10 '21 at 19:48

1 Answers1

1

First argument usually is previous value, but You can initialize first argument using initialValue.

Read manual

reduce((previousValue, currentValue, currentIndex, array) => { ... }, initialValue)

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, },
      ],
    },
  ];
  
  
courses.forEach(course => {
  course.totalExercises = course.parts.reduce(
    (accumulator, part) => accumulator + part.exercises,
    0  // initial value for accumulator
  );
  
  console.log(course.name, ', total exercises:', course.totalExercises);
});

console.log(courses);

To get an idea how reduce works based on failure-success try to put console.log inside reducer:

const parts = [ {exercises: 5}, {exercises: 3}, {exercises: 7} ];

const result = parts.reduce(
  (prev, curr, _, arr) => {
    console.log('prev: ', prev);
    console.log('curr: ', curr);
    console.log('--------');
    return prev + curr.exercises;
  },
  0
);

console.log('result:', result);
num8er
  • 18,604
  • 3
  • 43
  • 57
  • @harry9345 reduce works like this: `[part1, part2, part3]`, `[part2, part3]`, `[part3]` - so it reduces array by providing reduced (removed) item, so at beginning `part1` and `part2` are objects, so Your previous value is like a state which holds last return, to avoid that I've initialized previousValue with 0 which made it look like: `[0, part1, part2,...]` – num8er Oct 10 '21 at 20:06