Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Javascript twoSum algorithm: Given an array of integers, return indices of the two numbers such that they add up to a specific target

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]
            }
        }
    }
};

console.log(twoSum([15, 7, 11, 2],9))
console.log(twoSum([3, 2, 4],6))
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Javascript #twoSum #Given #array #return #indices #numbers #add #specific #target
ADD COMMENT
Topic
Name
8+8 =