SOURCE

console 命令行工具 X clear

                    
>
console
const btn1 = document.getElementById('btn1')
btn1.addEventListener('click',()=>{
    console.log('>>>>>')
})

function func1(arr, n) {
    let left = 0;
    let right = arr.length - 1;
    let zx = arr[left] < arr[right];

    while (left <= right) {
        const mid = left + Math.floor((right - left) / 2);

        if (arr[mid] === n) {
            return mid; // 找到了n,返回位置
        } else if (zx ? arr[mid] < n : arr[mid] > n) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    // 如果未找到n,此时left指向应插入的位置
    arr.splice(left, 0, n);
    return left;
}

// 示例用法(增序数组)
const increasingArr = [1, 3, 5, 7, 9];
const n = 6;

const index = func1(increasingArr, n);
console.log(`数字 ${n} 应插入到位置 ${index}`);
console.log(increasingArr); // 输出 [1, 3, 5, 6, 7, 9]

// 示例用法(减序数组)
const decreasingArr = [9, 7, 5, 3, 1];
const n2 = 6;

const index2 = func1(decreasingArr, n2);
console.log(`数字 ${n2} 应插入到位置 ${index2}`);
console.log(decreasingArr); // 输出 [9, 7, 6, 5, 3, 1]

<div id="parent">
    <div id="btn1" ></div>
</div>
#parent {
    width: 200px;
    height: 200px;
    background: #999;
    pointer-events: none;
}

#btn1 {
    width: 40px;
    height: 40px;
    background: #fe1;
}