items[items.length - 1] is how code read the last item for years. at(-1) says the same thing directly, and with(index, value) is its partner for immutable updates: it returns a copy with one item replaced, the way a state update in a UI should, without touching the original array.
Write Array.prototype.myAt(index) and Array.prototype.myWith(index, value) in at-with.js. They must behave like the built-in at and with for every case below.
Array.prototype.myAt=functionmyAt(index) // returns an item or undefinedArray.prototype.myWith = functionmyWith(index, value) // returns a new array
index: a position. A negative index counts back from the end: -1 is the last item. It is converted to a whole number first: the fraction is dropped toward zero, and NaN or a missing index means 0.
value (myWith only): the item to put at that position.
myAt returns the item at the index; a negative index counts from the end.
myAt returns undefined for an index outside the array (including Infinity), for an empty array, and for a hole.
Both convert the index: "1" and 1.7 are 1, -1.5 is -1, NaN, null, "x", -0.5 and a missing index are 0. An index that is a BigInt or a Symbol, or an object whose valueOf returns one, throws a TypeError, as the built-ins do ([5].at(0n) throws).
myWith returns a new array of the same length with the item at the index replaced, and leaves the original unchanged. A negative index counts from the end.
myWith throws a RangeError for an index outside the array, including any index of an empty array.
myWith returns a packed array: every hole of the source becomes undefined in the result. The new value is stored as it is, even undefined.
Both work on any object with a length (an array-like object, or a string, for myAt), called with Array.prototype.myAt.call(…), and read length once. length is converted to a whole number from 0 up: "2" is 2, 2.9 is 2, -1 is 0; a BigInt or Symbol length throws a TypeError. myWith always returns a real array.
Both throw a TypeError when called on null or undefined.
Plain JavaScript in a browser page; the tests call Array.prototype.myAt and Array.prototype.myWith from at-with.js.
at-with.js starts with "use strict". Keep it: in strict mode, this inside the methods is exactly the value they were called on, so a call on null can be detected.
Not allowed: the built-in at, with, slice, splice and toSpliced. The tests delete them from Array.prototype while your methods run.