Extract the index which has the next greater prime
let x = - 1 for ( let j = 0 ; j < primes . length ; j ++ ) { if ( arr [ i ] == primes [ j ] ) { x = j break } else if ( arr [ i ] < primes [ j ] ) { x = j break } }