Allow Negative Array Indexes
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write withNegativeIndexes(array): return a Proxy around the array that also understands negative indexes, counting from the end, as in Python. Reading index -1 gives the last item, -2 the one before, and so on; a negative index that reaches past the start gives undefined. Writing to a negative index replaces that item in the original array. Everything else behaves exactly as the array does: zero and positive indexes, length, Array.isArray, spread and methods such as push, map and includes, with every change landing in the original array. Remember that property keys reach a Proxy as strings, so index -1 arrives as the string "-1".
Your code must pass
- Reads from the end with negative indexes
- Writes with negative indexes
- Gives undefined past the start
- Still works like the original array