0

I have an array like this:

parts: [
      {
        name: 'Fundamentals of React',
        exercises: 10,
        id: 1
      },
      {
        name: 'Using props to pass data',
        exercises: 7,
        id: 2
      },
      {
        name: 'State of a component',
        exercises: 14,
        id: 3
      },
      {
        name: 'Redux',
        exercises: 11,
        id: 4
      }
    ]

I would like to get the sum of the excercises. I'm trying to accomplish it thusly:

const Total = (props) => {

  const total = props.parts.reduce( (prev, cur) => {
    
    console.log('prev.excercises : ', prev.exercises)
    console.log('cur.excercises : ', cur.exercises)

    return prev.exercises + cur.exercises 
  })

  return (
    <>
      <p>Number of exercises {total}</p>
    </>
  )
}

But this results in NaN. The console shows why this result occurs, but not why the prev.excercises value is acting weird:

prev.excercises :  10
cur.excercises :  7
prev.excercises :  undefined
cur.excercises :  14
prev.excercises :  undefined
cur.excercises :  11

I'm really new to js, so there is probably something really simple I'm doing wrong.. appreciate the help in advance!

6
  • 2
    You're not giving an initial value to reduce, thus prev will change from being an object the first iteration to being a number the second one. Please review how Array#reduce works because you're trying to do two conflicting things with it right now which would of course lead to weird results. Commented Feb 11, 2022 at 8:55
  • 1
    Did you look at the documentation for reduce. You don't have an initial variable. prev.excercises doesn't exist. Commented Feb 11, 2022 at 8:55
  • @Andy exists only the first time the function is called because without an explicit initial value, the first item in the array becomes the initial value. Commented Feb 11, 2022 at 8:58
  • you should change return prev.exercises + cur.exercises to return prev + cur.exercises Commented Feb 11, 2022 at 8:59
  • Technically, yes, but introducing steps to ensure that doesn't happen is important. Commented Feb 11, 2022 at 9:01

2 Answers 2

2

You're trying to access it as an object but are returning it as an integer

const Total = (props) => {

  const total = props.parts.reduce( (prev, cur) => {

    console.log('prev.excercises : ', prev.exercises)
    console.log('cur.excercises : ', cur.exercises)

    return {exercises : prev.exercises + cur.exercises}
  })

If you don't want to return object you can do it like this

const total = props.parts.reduce( (prev,    cur) => {
prev=typeof prev==="object" ?  prev.exercises:prev
console.log('prev.excercises : ', prev)
console.log('cur.excercises : ', cur.exercises)

return  prev+ cur.exercises  })
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You! This works. But as I sed, I'm new to javascript syntax and I really don't understand why this helps. I shall dive in to the documentation of reduce! Maybe there I will understand, why prev and cur are acting differently.
All the best in your journey learning javascript
0

I also got it working like this, but I guess Ahmed Ali's answer is more to the nose!

const Total = (props) => {

  const total = props.parts.map(part => part.exercises).
  reduce((prev, cur) => {
    console.log('prev : ', prev)
    console.log('cur : ', cur)
    return prev + cur
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.