[Home] >  Number >  Calculate the mod of collection index

Calculate the mod of collection index

JavaScript version

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

TypeScript version

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

Examples

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