# 2sum LeetCode Solution: JavaScript

`Input: nums = [2,7,11,15], target = 9Output: [0,1]Output: Because nums + nums == 9, we return [0, 1].`
`var twoSum = function(nums, target) {    const previousNumbers = {};    }`
`var twoSum = function(nums, target) {    const previousNumbers = {};    for(let i = 0; i < nums.length; i++) {        const currentNumber = nums[i];        const numberNeeded = target - currentNumber        const index2 = previousNumbers[numberNeeded]  }}`
`nums = [2,7,11,15] target = 9var twoSum = function(nums, target) {    const previousNumbers = {};    for(let i = 0; i < nums.length; i++) {        const currentNumber = nums[i];        const numberNeeded = target - currentNumber        const index2 = previousNumbers[numberNeeded]     if (index2 != null) {        return [index2, i]    } else {            previousNumbers[currentNumber] = i        }    }}`
`currentNumber = 2;numberNeeded = 7; (9 - 2 = 7)index2 = previousNumbers === null(does not exist yet)*we will hit our else statement first iteration*previousNumbers = 0previousNumbers = {2:0}`
`currentNumber = 7;numberNeeded = 2; (9 - 7 = 2)index2 = previousNumbers = 0*since index2 has a value this iteration our first condition is satisfied and we return a solution!*return [index2, i]     --  index2 = 0 and i = 1previousNumbers = {2:0}`

--

--