Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

two sum js

function sumDigit(nums, target) {
  let n = nums.length;

  for(let i = 0; i < n-1; i++) {
   for(let j = i+1; j < n; j++) {
    if(nums[i] + nums[j] === target) return [i,j]
   }
  }
  return []
}

sumDigit([3,2,4], 6)

// n-1 = last index dropped if -> n+1 add new index
// nums[i] = 332
// nums[j] = 244
// nums[i] + nums[j] -> 3+2 = 5 | 3+4 = 7 | 2+4 = 6
Source by leetcode.com #
 
PREVIOUS NEXT
Tagged: #sum #js
ADD COMMENT
Topic
Name
9+4 =