AI basics & verificationPractical AI workflows

Why does a previous-item button fail with a negative remainder?

In JavaScript, -1%3 is -1, not 2. Using it as an ordinary array index does not select C in a three-item list. Define a nonempty list and safe-integer inputs, then normalize the remainder into the valid index range.

Ferris wheel with colourful cabins against a blue sky
Illustrative photo: Daniel Ríos · Unsplash LicenseStock illustration, not a software screenshot, tested device or evidence of this exercise.

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.

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.

Observed three-item cycle cases
OffsetJavaScript remainderValid indexItem
-4-12C
-3Negative zero0A
-1-12C
300A

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.

Copyable reference function

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)]); // C
Executed for the integer cases described here; no DOM button, network request or file processing is included.

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

Further reading & sources