/
1_TwoSum.js
50 lines (43 loc) · 999 Bytes
/
1_TwoSum.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const assert = require('assert');
/**
*
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target)
{
if (nums == undefined)
{
return;
}
let results = [];
for (let i = 0; i < nums.length; i++)
{
for (let j = 1; j < nums.length; j++)
{
if (i == j)
{
continue;
}
if ((nums[i] + nums[j] ) == target)
{
results.push(i);
results.push(j);
return results;
}
}
}
};
describe('1_TwoSum.js', function ()
{
it('should return [0, 1]]', function ()
{
let nums = [2,7,11,15];
let target = 9;
var results = twoSum(nums, target);
assert.deepEqual([0, 1], results);
});
});