Pair of elements from a specified array whose sum equals a specific target number
<p>I am in mid of my JavaScript session. Find this code in my coding exercise. I understand the logic but I didn't get this map[nums[x]] condition.</p>

<pre><code>function twoSum(nums, target_num) {  
    var map = [];  
    var indexnum = [];  

    for (var x = 0; x &lt; nums.length; x++)  
    {  
        if (map[nums[x]] != null)  
        // what they meant by map[nums[x]]
        {  
            index = map[nums[x]];  
            indexnum[0] = index+1;  
            indexnum[1] = x+1;  
            break;  
        }  
        else  
        {  
            map[target_num - nums[x]] = x;  
        }  
    }  
    return indexnum;  
    }  
console.log(twoSum([10,20,10,40,50,60,70],50));
</code></pre>

<p>I am trying to get the Pair of elements from a specified array whose sum equals a specific target number. I have written below code.</p>

<pre><code>function arraypair(array,sum){
        for (i = 0;i &lt; array.length;i++) {
            var first = array[i];
            for (j = i + 1;j &lt; array.length;j++) {
                var second = array[j];

                if ((first + second) == sum) {
            alert('First: ' + first + ' Second ' + second + ' SUM ' + sum);
            console.log('First: ' + first + ' Second ' + second);
                }
            }

        }
}

var a = [2, 4, 3, 5, 6, -2, 4, 7, 8, 9];

arraypair(a,7);
</code></pre>

<p>Is there any optimized way than above two solutions? Can some one explain the first solution what exactly map[nums[x]] this condition points to?</p>