You've reached for arr[arr.length - 1] to grab the last element of an array more than once. Array.prototype.at is the modern, less noisy way to do that — it accepts negative indices that count back from the end.
Implement arrayAt(arr, index). When index is 0 or positive, return the element at that position. When index is negative, count from the end: -1 is the last element, -2 is the second-to-last, and so on. If the resolved position falls outside the array, return undefined.
function arrayAt(arr, index) {
// returns arr[index] for index >= 0,
// arr[arr.length + index] for index < 0,
// or undefined if out of range.
}
arrayAt(['a', 'b', 'c', 'd'], 1); // 'b'
arrayAt(['a', 'b', 'c', 'd'], -1); // 'd'
arrayAt(['a', 'b', 'c'], 10); // undefined
arrayAt(['a', 'b', 'c'], -10); // undefined
arrayAt(['a', 'b', 'c'], 1.7); // 'b' — index truncates toward zero
-1 is the last element, not "one before index 0."undefined, not throw.1.7 becomes 1, -1.9 becomes -1.undefined, regardless of the index.arrayAt is a read-only lookup.