programming-examples/js/Array/Write a JavaScript program to find a pair of elements (indices of the two numbers) from an given array whose sum equals a specific target number..js

21 lines
419 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function twoSum(nums, target_num) {
var map = [];
var indexnum = [];
for (var x = 0; x < nums.length; x++)
{
if (map[nums[x]] != null)
{
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));