[Home] > Snippets  > Languages  > JavaScript  > Numbers  >  Calculate the mod of collection index

Calculate the mod of collection index

JavaScript

const mod = (a, b) => ((a % b) + b) % b

TypeScript

const mod = (a: number, b: number): number => ((a % b) + b) % b

Examples

mod(-1, 5) // 4
mod(3, 5) // 3
mod(6, 5) // 1