JSFiddle - React, Tailwind, and code Playground

Binary search algorithm for finding a value at specified index in two independent sorted arrays (index = element at specified index of merged, sorted array)

by Yurii Predborskyi

HTML

Github repository:
https://github.com/Yuri-Predborskiy/js-fiddles-for-leetcode/tree/master/4.%20Median%20of%20Two%20Sorted%20Arrays

Original code from GeeksForGeeks:
https://ide.geeksforgeeks.org/DJu7to

Original article:
K-th Element of Two Sorted Arrays
https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/

For realization see Javascript tab.

JavaScript

let pass = 0;
let logging = false;
let logs = 0;

function log(...messages) {
    if (logging) console.log(messages.join(' '));
}

function getIndexElementInTwoSortedArrays(arrLeft, arrRight, index, startLeft = 0, endLeft = arrLeft.length, startRight = 0, endRight = arrRight.length) {
    let shiftLeft = Math.floor((endLeft - startLeft) / 2);
    let shiftRight = Math.floor((endRight - startRight) / 2);

    log('pass', pass++, 'startLeft', startLeft, 'endLeft', endLeft, 'startRight', startRight, 'endRight', endRight, 'index', index);

    if (startLeft === endLeft) {
        log('reached end of array 1, element not found, so it should be at index startRight + index in arrRight');
        log(`looking at arrRight[${startRight + index}], value is ${arrRight[startRight + index]}`);
        return arrRight[startRight + index];
    }
    if (startRight === endRight) {
        log('reached end of array 2, element not found, so it should be at index startLeft + index in arrLeft');
        log(`looking at arrLeft[${startLeft + index}], value is ${arrLeft[startLeft + index]}`);
        return arrLeft[startLeft + index];
    }

    if (shiftLeft + shiftRight < index) {
        // log(`shiftLeft (${shiftLeft}) + shiftRight (${shiftRight}) < index (${index})`);
        log(`mids (${shiftLeft} + ${shiftRight} = ${shiftLeft + shiftRight}) < index (${index}), lets increase start and limit index`);
        if (arrLeft[startLeft + shiftLeft] < arrRight[startRight + shiftRight]) {
            // log(`arrLeft[startLeft + shiftLeft] ${arrLeft[startLeft + shiftLeft]} < arrRight[startRight + shiftRight] ${arrRight[startRight + shiftRight]}`);
            log('limiting start of left and limiting index - shiftLeft - 1');
            return getIndexElementInTwoSortedArrays(arrLeft, arrRight, index - shiftLeft - 1, startLeft + shiftLeft + 1, endLeft, startRight, endRight);
        } else {
            // log(`arrLeft[startLeft + shiftLeft] ${arrLeft[startLeft + shiftLeft]} >=...