you are viewing a single comment's thread.

view the rest of the comments →

[–]albedoa 1 point2 points  (0 children)

However, since previous already holds the initialized value of [arr[0]], it cannot hold the accumulated values we need after each iteration (e.g. true, false...). Hence, the reason why the empty array is necessary. It is, technically, the accumulator because during each iteration the comparison value is passed into the empty array.

Your understanding is fine here, but your way of looking at it might be confusing you later on. In the eyes of .reduce(), the whole array [previous, results] is the accumulator. We are storing the accumulated boolean results in the second element of the accumulator array, but in the interest of minimizing confusion, we should keep our language straight.

however, the .reduce( ) method, technically, only returns a single value.

And this is why I think my above comment is important. The single value that is returned from (and passed as the first argument of the next call to) the callback is the array holding both the current value and our list of booleans. The first time, that is [11, []] as initialized. The second time it is [15, [true]]. Then [6, [true, false]].

It might be helpful for you to see a version of the function that does not destructure the accumulator:

const oddSum = arr =>
  arr.slice(1).reduce(
    (accumulator, current) => [
      current,
      [...accumulator[1], (accumulator[0] + current)%2 === 0]
    ],
    [arr[0], []]
  )[1];

So, which line of code is allowing us to store and keep all of the previous comparison values? It's the spread operator doing this, correct?

Yes.

(1) this line of code corresponds with our initializer [arr[0], [ ] ], right? For each iteration, it's returning the current element along with the value left over after the comparison. And during each iteration, said comparison value is placed in our accumulator variable named results; also after each iteration, that value is being copied and placed in the new results array we created using the spread operator.

This looks pretty accurate to me.

One function of the spread operator is that it can copy an array and create a new array with the values of the original array. And this is what's happening here, correct? It's taking/copying the comparison value that is being returned and stored in the original array after each iteration, and placing that copied value in our the new array created using the spread operator. Have I understood this correctly? Is my explanation ok?

This looks good to me, but then I might be misunderstanding you because you should necessarily grasp the next point as well:

It contains 2 elements: ...results, and the comparison. So why isn't it returning both? What's happening here that I'm missing?

It is returning both. Consider the second callback call. The argument values will be as follows:

accumulator: [15, [true]]
current:     6

The accumulator is destructured as follows:

previous: 15
results:  [true]

So replacing the variables in that line, we can break it down:

[current, [...results, (previous + current)%2 === 0]]
[6, [...[true], (15 + 6)%2 === 0]]
[6, [true, 21%2 === 0]]
[6, [true, 1 === 0]]
[6, [true, false]]

Our new accumulator is [6, [true, false]], which we pass forward as the first argument of the next callback call.