[https://drive.google.com/file/d/12qIWazKzBr1yJGpo1z106euXVJMKMwEa/view?usp=sharing][1]I am struggling to find the correct indices of two numbers in an array whose sum equals a target number provided as an argument. I attempted to solve this using a for loop, but my code is not returning the accurate indices. For example, if the first two numbers in the array are both 1 and the target is set as 2, I am getting the answer as [0,0], however, I want it to be [0,1]
let arr = [1,1,8,9,7,22,6]
var twoSum = function(nums, target) {
for(i=0;i<nums.length;i++){
for(j=i+1;j<nums.length;j++)
if(nums[i] + nums[j]==target){
return [nums.indexOf(nums[i]) , nums.indexOf(nums[j])]
}
}
};
console.log(twoSum(arr, 2))
i am expecting output as [0,1]
but i am getting output as [0,0]
[1]: https://i.sstatic.net/n1jo7.png