I'm working on a problem below, but not sure why my second for loop does console.log anything. I currently get a result, [], and when I work on loops I sometimes confused because the output returns empty result. So why my second for loop is not executed and why I'm not able to push the result to result array?
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order.
Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
function a(nums, target) {
let result = [];
for (let i = 0; i < nums.length -1; i++){
let firstNum = nums[i];
console.log(i)
for (let j = i + 1; j < nums.length -1; j++){
console.log(j)
let secondNum = nums[j];
let sum = firstNum + secondNum;
if (sum === target) {
return result.push(i, j);
}
}
}
return result;
};
a([2,7,11,15], 9)
Push return only the length of the array, try like below,