-1
var twoSum = function(nums, target) {
    
    
    let index = 0
    let i = 1
    while (index <= nums.length - 1) {
            while (i <= nums.length - 1) {
                if (nums[index] + nums[i] === target) {
                    if (nums[index] === nums[i]) {
                        i = i + 1
                    }
                  return([index, i])
                } else {
                    i = i + 1
                }
            }
        index = index + 1
        i = 0
    }
};

twoSum([3, 3], 6)

expected output: [0, 1] received: [0, 2]

description: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Karen
  • 187
  • 6
  • 1
    you might be better off with `for` loops rather than `while` – gog Oct 02 '22 at 08:02
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Oct 02 '22 at 08:17

6 Answers6

1

@lucumt already pointed out that you should leave out two lines of your code. However, a slightly more conventional way of expressing your function would involve for() loops as they would make the code slightly more readable than the chosen while() construct. The inner loop should also always start at index+1, see below:

var twoSum = function(nums, target) {
  const res=[];
  for (let index=0; index <nums.length; index++) {
    for (let i=index+1;i <nums.length; i++) {
      if (nums[index] + nums[i] === target ) res.push([index, i]);
    }
  }
  return res
}
console.log(twoSum([3, 3], 6)) // [[0,1]]
// second example:
console.log(twoSum([1,6,4,7,3,2,8,3], 6)) // [[2,5],[4,7]]

My snippet will return all possible sums in an array of arrays. If you only want the first solution, simply do twoSum(arr,targetNum)[0].

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
1

You need to remove below code when you found the matched result

    if (nums[index] === nums[i]) {
        i = i + 1
    }

var twoSum = function(nums, target) {
    
    
    let index = 0
    let i = 1
    while (index <= nums.length - 1) {
            while (i <= nums.length - 1) {
                if (nums[index] + nums[i] === target) {
                  return([index, i])
                } else {
                    i = i + 1
                }
            }
        index = index + 1
        i = 0
    }
};

console.log(twoSum([3, 2, 0,1,4], 6))
flyingfox
  • 13,414
  • 3
  • 24
  • 39
1

Just remove this block.

                    if (nums[index] === nums[i]) {
                        i = i + 1
                    }

BTW, why you write this code from the start. You are not getting all the possible numbers. No matter how long the input array is, you just return 2 indexes.

yansainity
  • 156
  • 6
1
let index = 0
while (index <= nums.length - 1) {
        let i = 0
        while (i++ <= nums.length - 1) {
            if (index!==i && nums[index] + nums[i] === target) {
              return([index, i])
            }
        }
    index++
}
SUSEENDHAR LAL
  • 141
  • 2
  • 3
0

You can use for-loop with the same logic because it looks more simple.

var twoSum = function(nums, target) {
    for(let i=0; i<nums.length; i++){
        for(let j=i+1; j<nums.length; j++){
            if(nums[i] + nums[j] === target){
                return [i, j]
            }
        }
    }
};
Arifur Rahaman
  • 534
  • 1
  • 2
  • 8
0
  
  let index = 0
  let i = 1
  while (index <= nums.length - 1) {
          while (i <= nums.length - 1) {
              if (nums[index] + nums[i] === target) {
                  // if (nums[index] === nums[i]) {
                  //     i = i + 1
                  // }
                return([index, i])
              } else {
                  i = i + 1
              }
          }
      index = index + 1
      i = 0
  }
};

console.log(twoSum([3, 3], 6));

Skip that "if" statement above it will increase your i's value for this case.