Linxo
12 بازدید۱۴۰۵/۲/۸

javascript

javascript/raw
12345678910111213141516171819
function binarySearch(ip, ranges) {
  let low = 0
  let high = ranges.length - 1

  while (low <= high) {
    const mid = Math.floor((low + high) / 2)
    const [start, end, , country] = ranges[mid]

    if (ip < start) {
      high = mid - 1
    } else if (ip > end) {
      low = mid + 1
    } else {
      return country
    }
  }

  return null
}