Iterate over the ranges
for ( let i = 0 ; i < r ; i ++ ) { let c = 0 ; let l = ranges [ i ] [ 0 ] ; let h = ranges [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . push ( c ) ; } for ( let i = 0 ; i < a . length ; i ++ ) { document . write ( a [ i ] + " " ) ; } }