This function always results in an empty array.
Here is a JavaScript implementation for solving the two sum problem:
function twoSum(nums, target) {
const map = {};
for (let i = 0; i < nums.length; i++) {
let comp = target - nums[i];
if (map[comp] !== undefined) {
return [map[comp], i];
} else {
map[comp] = i;
}
}
return [];
}
console.log(twoSum([2, 7, 11, 15], 9));