For my current project, I've been tasked with writing a function that accepts an array of numbers along with a target number.
The goal is to identify two numbers in the array that can be added together to reach the target value, and then return the indices of these two numbers as a tuplet (index1, index2).
To tackle this challenge, I decided to use a nested for loop to compare each element in the array to every other element. I then included an if statement to pinpoint the indices when the desired sum is achieved.
Unfortunately, it seems like something isn't quite right with my code. It could be an issue with how I am returning the results as a tuplet, or maybe the logic for obtaining the correct indices needs tweaking. Any suggestions, advice, or hints on resolving this puzzle would be greatly appreciated. Thank you!
function twoSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) {
return [numbers.indexOf(i), numbers.indexOf(j)];
}
}
}
}