The algorithm probably says something like:
x β‘ 1 (mod N)
(mod N) , , . , . x β‘ 1 , x 1 . N , 1 x .
x β‘ 1 (mod N) x % N === 1 JavaScript, x . , : , -1 β‘ 1 (mod 2), (-1) % 2 === -1, 1, "" .
, x , :
x β‘ 1 (mod N)
β x - 1 β‘ 0 (mod N)
x - 1, 0, , N, modulo:
if ((x - 1) % N === 0) {
...
}