# Basic Algorithm Scripting - Where do I Belong

Hello all,

I am facing this problem . Could you please give a hint how I have to solve it?

My code so far

``````var twoSum = function(nums, target) {
let gain=[ ];
for (let i=0; i<nums.length; i++) {
if (nums[i]+nums[i+1]===target) {
gain.unshift(i,i+1)
console.log(nums[i],nums[i+1])

}

return console.log(gain)
}
};

twoSum([3,2,4],6)
``````

Side note - you shouldn’t be using `var` at this stage in the curriculum. It is a legacy feature of JavaScript. `let` and `const` are preferred.

hi there, you seem to have deleted the link to the challenge just now.

Thank you for the reply, but it doesn’t affect to the result

What is happening?

What do you want to happen instead?

What have you tried to fix the code?

``````let twoSum = function(nums, target) {
let gain=[];
for (let i=0; i<nums.length; i++) {
if (nums[i]+nums[i+1]===target) {
gain.unshift(i,i+1)
console.log(nums[i],nums[i+1])

}

return console.log(gain)
}
};

twoSum([3,2,4],6)
``````

this is my code so far. The problem is from Leetcode

We aren’t magic ‘code fixit’ machines. We can’t help without knowing what is supposed to happen

Aaa, sorry for not giving clear help. It is my first time asking help in the forum.

This code should show indicies of numbers that are equal to the target when they are combined.

It works if in “nums” I give ([2,4,8],6). It finds first two values of numbers only that are equal to 6 when they are added.
But if I give ([8,2,4], 6) it can’t find it

This return statement is inside of a loop, so you will terminate the loop early.

Also note that if you return a `console.log()`, then you return the return value of that function call, which is `undefined`.

Do you want any pairs or adjacent pairs only?

Any pairs that are equal to target when they are combined

It seems like you don’t have enough loops for that with your current strategy?

This one helped a little bit. Year, you are right. I didn’t place return out of loop, also console.log is extra. But it doesn’t work when I call a function with the input

twoSum([3,2,3], 6)

Right, because you are explicitly only considering adjacent entries with your code above.

I see. I will try to figure out for some time. Thanks a lot

Solve it bro.

This is my final code:

``````
let twoSum = function(nums, target) {
let gain=[];
for (let i=0; i<nums.length; i++) {
for (let j=i+1; j<nums.length; j++) {
if (nums[i]+nums[j]==target) {
gain.unshift(i,j)
console.log(nums[i],nums[j])
}
}

}
return console.log(gain)
};

twoSum([3,2,3],6)
``````

Congrats! If you are interested in bonus points - can you research and implement the one loop solution?

Thank you for your help. Yeah, sure.

Sorry another question. I have to always use console.log in VSC in order to see my results, wihout it doesn’t show anything

Right, I would expect that. Nothing gets “print”/"log"ed unless you explicitly ask for that.

What I don’t understand is that in python If I am not mistaken when you use return it gvies the answer in the terminal, but with JS it doesn’t work in my VSC. Maybe I have some problems in the settings of the VSC