A carousel shows a row of cards and highlights the three in the middle. A playlist editor swaps the middle of one list with the middle of another. Both need the same two steps: find where the "middle three" starts for an array of any length, and copy or replace exactly that range. The arithmetic is short, but an even length and a too-short array are where most first attempts go wrong.
Write middleThree(array) and replaceMiddle(target, source) in middle.js.
middleThree returns the three centred items of an odd-length array.
For an even length, it returns the three items that lean toward the start (in 4 items, indexes 0, 1 and 2).
For exactly 3 items, it returns a copy of all three.
An array with fewer than 3 items makes either function throw a RangeError.
replaceMiddle replaces exactly the middle three items of target with the middle three of source, keeps every other item of target in place, and keeps target's length.
The two arrays can have different lengths, odd or even.
Both functions return new arrays, never change their inputs, and keep objects as they are.
If the middle three start at index s in an array of length n, how many items are before them and how many after? What makes those two numbers as equal as possible?
Which array method removes some items and inserts others in their place?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.