-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (28 loc) · 803 Bytes
/
Copy pathscript.js
File metadata and controls
32 lines (28 loc) · 803 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
26
27
28
29
30
31
32
/**
* @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 output = [];
let selectedNum;
for (let i = 0; i < nums.length; i++) {
selectedNum = nums[i];
for (let j = 1; j < nums.length; j++) {
if ((selectedNum + nums[j] === target) && (i !== j)) {
output.push(i, j);
break;
}
}
if (output.length !== 0) {
break;
}
}
return output;
};
console.log('numArrOne', twoSum(numArrOne, targetOne));
console.log('numArrTwo', twoSum(numArrTwo, targetTwo));
console.log('numArrThree', twoSum(numArrThree, targetThree));