二和算法:我的解决方案返回未定义的结果,但打印预期的输出



在我疯狂之前,我需要一些帮助,因为这对我来说没有意义。所以如果有人能把我从这个洞里救出来,我会非常感激。

这就是问题所在:给定一个整数数组nums和一个整数target,返回这两个数字的索引,使它们加起来成为目标。您可以假设每个输入只有一个解决方案,并且不能两次使用同一个元素。你可以按任何顺序返回答案

这是我的代码:

let numsCopy = [];
// new array to push the numbers index
let arrayResult = []
let currentNumberIndex = 0
var twoSum = function(nums, target) {
console.log('inside arrow function',nums, target);
// Copy the array
numsCopy = nums.slice(0)
// return fHelper(nums, target)
// let test = () => fHelper(nums, target)
console.log(fHelper(nums, target));
};
function fHelper(nums, target){
console.log('inside fhelper',nums, target);
// exit condition
if (nums.length < 1) return
// grab the first element and mutate our nums
let currentNumber = nums.shift()
for (var i = 1; i < numsCopy.length; i++) {
// check if the current number plus number in the next position of the array
// equals the target we are going to push the index of these numbers to the
// arrayResult
console.log('i: ', i, ' == ', 'numsCopy.length:', numsCopy.length);
console.log('nums length is: ', nums.length);
console.log(`${currentNumber} + ${numsCopy[i]} equals target ${target}?`)
if (currentNumber + numsCopy[i] === target) {
console.log('yes');
arrayResult.push(currentNumberIndex)
arrayResult.push(i)
console.log(arrayResult, 'typeof', typeof arrayResult, 'is array? ',Array.isArray(arrayResult));
return arrayResult
}
console.log('no');
}
console.log('currentNumberIndex: ', currentNumberIndex);
currentNumberIndex++
// recursion. we call again the function
//array nums get shorter every time with the shift()
fHelper(nums, target)
}
// Hello after hours of reading and trying to figured out and understand why my solution in some cases is returning undefined.
// This is my code and I dont understand why is returning undefined

// case 1
// twoSum([2,7,11,15], 9) // Expected output = [0,1]
// case 2
twoSum([3,2,4], 6) // in this case return undefined. Expected output= [1,2]
// case 3
// twoSum([3,3], 6) // Expected output= [0,1]
// case 4
// twoSum([3,3,2,8], 10) // in this case returns undefined. Expected output= [2,3]
// case 5
// twoSum([3,2,3], 6) // Expected output= [0,2]

函数末尾需要一个return语句。

function fHelper(nums, target) {
// ...
return fHelper(nums, target); // <<<<<<<<<
}

let numsCopy = [];
// new array to push the numbers index
let arrayResult = []
let currentNumberIndex = 0
var twoSum = function(nums, target) {
console.log('inside arrow function',nums, target);
// Copy the array
numsCopy = nums.slice(0)
// return fHelper(nums, target)
// let test = () => fHelper(nums, target)
console.log(fHelper(nums, target));
};
function fHelper(nums, target){
console.log('inside fhelper',nums, target);
// exit condition
if (nums.length < 1) return
// grab the first element and mutate our nums
let currentNumber = nums.shift()
for (var i = 1; i < numsCopy.length; i++) {
// check if the current number plus number in the next position of the array
// equals the target we are going to push the index of these numbers to the
// arrayResult
console.log('i: ', i, ' == ', 'numsCopy.length:', numsCopy.length);
console.log('nums length is: ', nums.length);
console.log(`${currentNumber} + ${numsCopy[i]} equals target ${target}?`)
if (currentNumber + numsCopy[i] === target) {
console.log('yes');
arrayResult.push(currentNumberIndex)
arrayResult.push(i)
console.log(arrayResult, 'typeof', typeof arrayResult, 'is array? ',Array.isArray(arrayResult));
return arrayResult
}
console.log('no');
}
console.log('currentNumberIndex: ', currentNumberIndex);
currentNumberIndex++
// recursion. we call again the function
//array nums get shorter every time with the shift()
return fHelper(nums, target)
}
// Hello after hours of reading and trying to figured out and understand why my solution in some cases is returning undefined.
// This is my code and I dont understand why is returning undefined

// case 1
// twoSum([2,7,11,15], 9) // Expected output = [0,1]
// case 2
twoSum([3,2,4], 6) // in this case return undefined. Expected output= [1,2]
// case 3
// twoSum([3,3], 6) // Expected output= [0,1]
// case 4
// twoSum([3,3,2,8], 10) // in this case returns undefined. Expected output= [2,3]
// case 5
// twoSum([3,2,3], 6) // Expected output= [0,2]
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新