// The JS % operater is REMAINDER not MODULO // For modulo behaviour use function mod(n, m) { return ((n % m) + m) % m; }