Key Takeaways
For positive length n, calculate r=offset%n. Return r+n for negative r, ordinary zero for either zero sign, and r otherwise. The reference gives wrap(-1,3)=2, wrap(-4,3)=2 and wrap(-3,3)=0. Reject invalid lengths, fractions, strings and nonfinite values rather than silently inventing an index.
- Do not use the negative result of -1%3 directly as an ordinary array index.Read the supporting section: Reproduce the boundary with three items
- Validate safe integers and positive length before normalizing the remainder.Read the supporting section: Put the input contract in the function
- Test multiple wraps, negative zero and invalid inputs, not only a single backwards step.Read the supporting section: Check multiple wraps and rejected inputs
In this article
Reproduce the boundary with three items
The authored list is ["A","B","C"], with current index 0 and an offset of -1 for one step backwards. The faulty items[-1 % items.length] yields undefined. A negative property name is not an ordinary array’s last-element index; do not confuse that operation with the separate at(-1) API.
The arithmetic and indexing were executed in Node. The faulty code is editorial teaching material, not measured output from an AI model. Retain the list and offset, inspect the computed index first, then inspect the element rather than interpreting undefined as missing source data.
The percent sign computes a remainder here
The MDN remainder reference distinguishes JavaScript remainder from a nonnegative modulo result. With positive divisor 3, -4%3 is still -1. Merely adding 3 to an arbitrary raw negative offset is not enough; take the remainder before adjusting its range.
The expression -3%3 also produces negative zero, often displayed as 0. The editorial check used Object.is to expose that boundary and the reference normalizes either zero sign to ordinary zero. Negative zero is not index -1, but explicit normalization keeps the result contract clear.
| Offset | JavaScript remainder | Valid index | Item |
|---|---|---|---|
| -4 | -1 | 2 | C |
| -3 | Negative zero | 0 | A |
| -1 | -1 | 2 | C |
| 3 | 0 | 0 | A |
Put the input contract in the function
This reference accepts only safe Number integers and a positive length. It takes the remainder before adding the positive length when necessary. If the caller first computes current+step, validate that computed value too, rather than checking only the two operands.
The string "1" is not silently accepted; fractions, Infinity and length zero are rejected. Another application may define a different contract, but should do so explicitly rather than treating coercion as an accidental feature.
function wrap(offset, length) {
if (!Number.isSafeInteger(offset) ||
!Number.isSafeInteger(length) || length <= 0) {
throw new TypeError("safe integers and positive length required");
}
const r = offset % length;
return r < 0 ? r + length : r === 0 ? 0 : r;
}
const items = ["A", "B", "C"];
console.log(items[wrap(-1, items.length)]); // CCheck multiple wraps and rejected inputs
At length 3, offsets [-7,-4,-3,-1,0,2,3,7] produce [2,2,0,2,0,2,0,1]. Check that every result is an integer in [0,3), then compare the selected item. A single forward click is a poor boundary test.
The negative-exponent and parentheses exercise concerns a different sign rule, not remainder semantics. Consult the JavaScript precision audit for another boundary: index normalization cannot recover numeric information already lost before this calculation.


