How to return new array from looping array indexes i and j

时间:2017-12-18 05:34:15

标签: javascript

Given an array of integers and I want to return indices of the two numbers such that they add up to a specific target. Assuming that each input would have exactly one solution, and I haven't used the same element twice.

I am using the brute force approach to Loop through each element x and find if there is another value that equals to target−x

code snippet:

var twoSum = function(nums, target) {
        for(var i ; i<nums.length ; i++){
            for(var j = i + 1; j<nums.length ; j++){
                if (nums[j]==target-nums[i]){
               // This is where I want to return new array 
                    return 
                    
                }
                
            }
        }
        
    };

1 个答案:

答案 0 :(得分:1)

You just want to return the two indices?

return [i, j];
相关问题