JS for the answer Two Sum ,but somewhere went wrong, asking for help


  • 0
    W

    /**

    • @param {number[]} nums

    • @param {number} target

    • @return {number[]}
      **/
      var twoSum = function(nums, target) {

      var result = new Array(0);
      //test the nums after the flag
      for (var i in nums){
      var n= i+1;
      var temp = 0;
      //length === 1
      if ( nums.length === 1){
      return;
      }
      do{
      temp = nums[i] + nums[n];
      if ( target === temp ){
      result.push(i, n);
      }
      n += 1;
      }while (n <= nums.lentgh);

      }
      return result;
      };

    Your input

    [2,4]
    6
    Your answer

    []
    Expected answer

    [0,1]

    I don't know what's wrong with this?
    someone can help me.?


  • 0
    W

    e, I find what's the matter, I shoud use the FOR but not the FOR IN
    the right thing :

    for(var i ; i<= nums.length; i ++) 
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.