The problem with % is that it is a remainder operator with truncated division, not a modulo one with floored division. When the divisor (i-1) becomes negative, so does the result. You can use
if (--i < 0) i = stuff.length - 1;
or
i = (i + stuff.length - 1) % stuff.length;
instead (which only work for input values of i in the expected range, though)
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Remainder
Remainder (%) - JavaScript | MDN
For two values of the same sign, ... which can make them differ by one unit of d. To obtain a modulo in JavaScript, in place of n % d, use ((n % d) + d) % d....
Videos
W3Schools
w3schools.com › js › js_arithmetic.asp
JavaScript Arithmetic
In arithmetic, the division of two integers produces a quotient and a remainder. In mathematics, the result of a modulo operation is the remainder of an arithmetic division.
Top answer 1 of 2
4
The problem with % is that it is a remainder operator with truncated division, not a modulo one with floored division. When the divisor (i-1) becomes negative, so does the result. You can use
if (--i < 0) i = stuff.length - 1;
or
i = (i + stuff.length - 1) % stuff.length;
instead (which only work for input values of i in the expected range, though)
2 of 2
1
If you want next() to increment i between 0 and 2 and prev() to decrement between 2 and 0 you can use the following:
next() {
this.props.dispatch(increaseCounter());
i = Math.min(i + 1, stuff.length - 1);
}
prev() {
this.props.dispatch(decreaseCounter());
i = Math.max(i - 1, 0);
}
LabEx
labex.io › tutorials › javascript-quotient-and-module-of-division-28277
Quotient and Module of Division in JavaScript | LabEx
We will learn how to implement this function using the Math.floor() and modulo operators, and use it to perform various division operations. By the end of this lab, you will have a solid understanding of how to use this function to solve problems involving division in your JavaScript programs. To practice coding, open the Terminal/SSH and type node. This code returns an array that consists of the quotient and remainder of the given numbers.
John Kavanagh
johnkavanagh.co.uk › home › articles › using the modulo operator in javascript
Using the Modulo Operator in JavaScript, by John Kavanagh
February 27, 2024 - Here, I'm using modulo to calculate the following index in an array, rolling back to 0 if we've reached the end of the array. The modulo operator in JavaScript is a simple yet powerful tool that extends beyond basic arithmetic. It's extremely useful for scenarios like checking divisibility, looping ...
AlgoCademy
algocademy.com › link
Modulo Operator in JavaScript | AlgoCademy
The key concept behind the modulo operator is its ability to determine the remainder of a division operation. This can be particularly useful in various programming scenarios: Even or Odd: You can use the modulo operator to check if a number is even or odd. If number % 2 equals 0, the number is even; otherwise, it is odd. Cycling Through Indices: When working with arrays, the modulo operator can help cycle through indices, ensuring they stay within bounds.
Mastering JS
masteringjs.io › tutorials › fundamentals › modulus
The Modulus Operator in JavaScript - Mastering JS
The modulus operator in JavaScript returns the remainder when the first operand is divided by the second operand. Here's what you need to know.
Mimo
mimo.org › glossary › javascript › modulo-operator
JavaScript Modulo Operator: Syntax, Usage, and Examples
Mastering the modulo operator will ... and array indexing. It also pairs well with concepts found in ecmascript, where numerical behavior stays consistent across environments. The syntax of the JavaScript modulo operator is similar to that of basic arithmetic operators: ... Since this operation produces a remainder, it's directly related to the concept of a quotient, which represents the whole-number result of the division ...