Сообщение от Alexandroppolus
|
то лучше всего найти индексы первого и последнего интервалов
|
<html><meta charset="utf-8">
<span> [8-16] [24-32] [40-48] [56-64] [72-80] [88-96] </span>
<br>
<input type="number" value="78" oninput="test()" /> и
<input type="number" value="90" oninput="test()" /> пересекается с
<input id="result" disabled="true" style="width: 500px;font-size:12px; "/>
<style>
body {text-align:center}
span {display:inline-block; padding:15px; font-size:18px; font-family:consolas}
input {width:100px; height:50px; font-size:35px; text-align:center}
</style>
<script>
window.onload = test;
var intervals = [
{beg: 8, end: 16},
{beg: 24, end: 32},
{beg: 40, end: 48},
{beg: 56, end: 64},
{beg: 72, end: 80},
{beg: 88, end: 96}
];
function test(value) {
var A = +document.querySelectorAll("input")[0].value;
var B = +document.querySelectorAll("input")[1].value;
function bin_search_range(arr) {
var min = 0;
var max = arr.length - 1;
var temp;
while (min < max) {
var mid = Math.floor((min + max) / 2);
if (arr[mid].end < A) min = mid + 1;
else max = mid
}
max = arr.length - 1;
temp = min
while (min < max) {
var mid = Math.floor((min + max) / 2);
if (arr[mid].beg < B) min = mid + 1;
else max = mid
}
if (arr[temp].beg <= B && arr[temp].end >= A) {
if (arr[min].beg <= B && arr[min].end >= A) ++min;
return JSON.stringify(arr.slice(temp,min))
}
return "..."
}
document.querySelector("#result").value = bin_search_range(intervals)
};
</script>
</html>