Skip to curriculum
Jump to the editor

3.4. Array.prototype.at and Array.prototype.with

Easy
Topics
10–20 min

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.

Signature

JAVASCRIPT
Array.prototype.myAt = function myAt(index)          // returns an item or undefined
Array.prototype.myWith = function myWith(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.

Requirements

  1. myAt returns the item at the index; a negative index counts from the end.
  2. myAt returns undefined for an index outside the array (including Infinity), for an empty array, and for a hole.
  3. 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).
  4. 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.
  5. myWith throws a RangeError for an index outside the array, including any index of an empty array.
  6. 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.
  7. 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.
  8. Both throw a TypeError when called on null or undefined.

Examples

JAVASCRIPT
const letters = ["a", "b", "c"];
letters.myAt(0);    // "a"
letters.myAt(-1);   // "c"
letters.myAt(5);    // undefined
letters.myAt(1.7);  // "b": the fraction is dropped
JAVASCRIPT
const scores = [10, 20, 30];
const updated = scores.myWith(-1, 35);
updated;            // [10, 20, 35]
scores;             // [10, 20, 30]: unchanged
JAVASCRIPT
[1, , 3].myWith(0, 0);  // [0, undefined, 3]: the hole is now a real undefined
[1, 2, 3].myWith(3, 0); // RangeError: there is no index 3

Environment

  • 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.

Hints

  1. Both methods turn the index into a position the same way. Could they share a helper?
  2. What does reading a hole give you, and how does that help myWith fill holes?

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.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…