Skip to curriculum
Jump to the editor

10.5. Negative indexes with a Proxy

Medium
Topics
15–30 min

In JavaScript, list[-1] is not the last item: it reads a property named "-1", which almost never exists, so you get undefined. list.at(-1) reads from the end, but only for reading; there is no list.at(-1) = x. Other languages let a negative index count from the end everywhere, and a Proxy can give an array the same behavior for every kind of access, without changing the array.

Write withNegativeIndexes(array) in negative-index.js. It returns a Proxy around the array where a negative whole-number index -k means position length - k, for reading, writing, the in operator and delete. Everything else behaves as on the plain array.

Signature

JAVASCRIPT
function withNegativeIndexes(array) // returns the wrapped array
  • array: the array to wrap. It is not copied: changes through the wrapper show in the original, and the other way round.
  • Returns an array (Array.isArray is true).

Requirements

  1. Positive indexes, length and all array methods work as usual.
  2. Reading list[-k] gives the item at length - k: list[-1] is the last item, list[-length] the first. A negative index before the start (-length - 1 or lower), or any negative index on an empty array, gives undefined.
  3. Writing list[-k] = value replaces the item at length - k, and length does not change. No property named "-1" is ever created on the array.
  4. Writing a negative index before the start throws a RangeError, and the array is unchanged.
  5. -k in list is true when position length - k holds an item, and false before the start or on a hole.
  6. delete list[-1] deletes the item at the last position, leaving a hole as delete on an array does. Before the start it does nothing and reports success.
  7. The meaning is worked out at the moment of each access: after a push, list[-1] is the new last item.
  8. Only canonical negative whole numbers count from the end. Keys such as "-1.5", "-01" and "-x" are ordinary property names, as on a plain array.

Examples

JAVASCRIPT
const list = withNegativeIndexes(["a", "b", "c"]);
list[-1];        // "c"
list[-3];        // "a"
list[-4];        // undefined
list[0];         // "a": positive indexes are unchanged
JAVASCRIPT
const list = withNegativeIndexes([1, 2, 3]);
list[-1] = 30;   // list is now [1, 2, 30], length still 3
list.push(4);
list[-1];        // 4
-4 in list;      // true
-5 in list;      // false
list[-9] = 0;    // RangeError
JAVASCRIPT
const original = ["x", "y"];
const list = withNegativeIndexes(original);
delete list[-1];
original;        // ["x", <hole>]: length is still 2
withNegativeIndexes([])[-1]; // undefined

Environment

  • Plain JavaScript in a browser page; the tests call the global withNegativeIndexes from negative-index.js.
  • Use a Proxy. Do not add methods or properties to the array.
  • Object.keys, for...in and Object.defineProperty with negative keys are not part of this problem.

Hints

  1. A trap receives the key as a string. How do you turn "-2" into a position, and how do you reject "-01"?
  2. Four operations are listed in requirements 2 to 6. Which trap handles each one?

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…