-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-two.js
More file actions
25 lines (22 loc) · 930 Bytes
/
script-two.js
File metadata and controls
25 lines (22 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const numArrOne = [2, 7, 11, 8, 14, 6], targetOne = 9; // [0, 1]
const numArrTwo = [3, 4, 2, 8, 15, 20], targetTwo = 6; // [1, 2]
const numArrThree = [16, 22, 7, 4, 8], targetThree = 12; // [3, 4]
const twoSum = (nums, target) => {
const comp = {};
for (let i = 0; i < nums.length; i++) {
// if the value of a property that is the current item in the array is greater that or equal to 0, then return an array with that value and the current index
if (comp[nums[i]] >= 0) {
return [comp[nums[i]], i];
}
// add the target minus the current index value in to comp as a key and its vaule as the current index
comp[target - nums[i]] = i;
}
};
console.log('numArrOne', twoSum(numArrOne, targetOne));
console.log('numArrTwo', twoSum(numArrTwo, targetTwo));
console.log('numArrThree', twoSum(numArrThree, targetThree));