Errata for Chapter 7, Challenge 5, Middle of array

The challenge is this:
Write a function that returns the middle element of an array. When array size is even, return the first of the two middle elememnts.

The word elements is written wrong.

The solution from the book doesn’t work for me.
Instead of:
return array[array.count/2]

my solution is:
return array[(array.count - 1) / 2]

best regards,

@djangolizer Both solutions return the array’s middle element if the array’s size is odd. Your solution returns the first of the two middle elements if the array’s size is even, while the book solution returns the second middle element for the same case.

I hope this helps!

This topic was automatically closed after 166 days. New replies are no longer allowed.