Challenge: Find the indices of two numbers in an array that add up to a specific target. For example, given nums = [2,7,11,15] and target = 9, the output should be [0,1] because nums[0] + nums[1] equals 9.
Having trouble with a JavaScript function called TwoSum that should solve this problem? You're not alone! It's frustrating when the code works in Python but returns undefined in JavaScript. I noticed that the function doesn't even enter the second loop, which is strange.
var twoSum = function(nums, target) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] >= target) {
continue;
}
for (let j = i; j < nums.length; j++) {
if (nums[j] >= target) {
continue;
}
if (nums[i] + nums[j] === target) {
const ans = [i, j];
return ans;
}
}
}
};
console.log(twoSum([2,7,11,15],9));
Any assistance would be greatly appreciated.