Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: add array and sort 
Javascript :: unhide is not working with radio button javascript 
Javascript :: Using an object of functions as a parameter into a function 
Javascript :: nested object data 
Javascript :: combining not selector with other jquery 
Javascript :: var oddOrEven = function(num) {}; 
Javascript :: prisma graphql n+1 problem solution 
Javascript :: json to dart dummy api 
Javascript :: disable scroll increment in react js number type 
Javascript :: fs.writefile example in aws lambda 
Javascript :: vue append component to div 
Javascript :: mogodb user get 
Javascript :: Backbone Model Fetch 
Javascript :: how to close bootstrap modal after save 
Javascript :: shrinkStringByRepeatFactor 
Javascript :: Backbone Model+Collection Minimal 
Javascript :: remove duplicate node 
Javascript :: javascript number reverse 
Javascript :: javascript declare multiple variables on one line 
Javascript :: react js tutorial for beginners 
Javascript :: append javascript variable to html 
Javascript :: traversing in jquery 
Javascript :: generator function in javascript 
Javascript :: babel core cdn 
Javascript :: open in new tab js html does not work on iphone 
Javascript :: what f a number exceeding 2^53 in javascript 
Javascript :: javascript Inside a regular function 
Javascript :: javascript Using yield to Pause Execution 
Javascript :: graphql type schema 
Javascript :: query middleware in express 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =