SOURCE

// let arr = [2, 7, 11, 15];
// let target = 9;


// let arr = [3, 2, 4];
// let target = 6;

let arr = [3,5];
let target = 6;

let twoSum = function (nums, target) {
    if (nums.length <= 1) return '数组长度不够';

    let result = [];

    for (let [index, item] of nums.entries()) {
        const res = deep(index, index + 1, nums, target);
        if (Array.isArray(res)) {
            return res;
        }
    }

    return '找不到'

    function deep(current, other, arr, target) {
        // 已经是末尾
        console.log(other);
        if (other > arr.length - 1) {
            return '超出数组长度'
        }
        // 当前数值大于target 调过
        if (arr[current] >= target) {
            return deep(arr[current + 1], arr[other + 1], arr);
        } else {
            // 两数相加等于当前target
            if (arr[current] + arr[other] === target) {
                return [current, other]
            } else {
                return deep(arr[current + 1], arr[other + 1], arr);
            }
        }
    }
};

console.log(twoSum(arr, target));
console 命令行工具 X clear

                    
>
console