Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Nested Data and Additional Exploration

Did we need to specify the default value of the accumulator to be equal to "[]"?

.

1 Answer

Steven Parker
Steven Parker
231,236 Points

If you don't supply an initial value, the operation will start with the first element in the accumulator, so the accumulator would not be an array.

Will Albertsen
Will Albertsen
13,155 Points

So how come the code still works normally even if an empty array is not set as the initial value? Since the first value of the accumulator would be the string 'The Day the Earth Stood Still', would calling the spread operator on it not get you the following result?

["T", "h", "e", " ", "D", "a", "y", " ", "t", "h", "e", " ", "E", "a", "r", "t", "h", " ", "S", "t", "o", "o", "d", " ", "S", "t", "i", "l", "l"]
Steven Parker
Steven Parker
231,236 Points

Separating the strings into individual characters is not the objective of the exercise.

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 Points

Hello from 2022! I am wondering the same thing. I ran the code both ways, and they both return an array of the string values, with or without the initial value. Am I missing something?

With initial value:

const flatMovies = movies.reduce((arr, innerMovies) => [...arr, ...innerMovies], []);

vs.

Without initial value:

const flatMovies = movies.reduce((arr, innerMovies) => [...arr, ...innerMovies]);

Both return an array

[
  'The Day the Earth Stood Still',
  'Superman',
  'Ghostbusters',
  'Finding Dory',
  'Jaws',
  'On the Waterfront'
]
Steven Parker
Steven Parker
231,236 Points

You're right, since the individual elements are arrays. I'm wondering if the example was different in 2019, since leaving off the initial value also doesn't produce the result shown by Will Albertsen now, either.