-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairwise.js
More file actions
66 lines (40 loc) · 1016 Bytes
/
pairwise.js
File metadata and controls
66 lines (40 loc) · 1016 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function pairwise(arr, arg) {
let res = [0];
for (let i = 0; i < arr.length; i++) {
let temp = [];
for (let j = 0; j < arr.length; j++) {
if (i != j && arr[i] + arr[j] == arg && arr[i] != null && arr[j] != null) {
temp.push([i, j])
console.log('arr[i] is ' + arr[i]);
console.log('arr[j] is ' + arr[j]);
console.log('i is ' + i);
console.log('j is ' + j);
console.log(' ');
}
}
if (temp.length > 1) {
let smallest = 0
console.log(temp)
for (let k = 0; k < temp.length; k++) {
if (temp[k][0] + temp[k][1] > smallest) { smallest = [temp[k][0], temp[k][1]] };
}
res.push(smallest[0] + smallest[1])
arr[smallest[0]] = null;
arr[smallest[1]] = null;
i = 0;
}
if (temp.length == 1) {
temp = temp[0]
res.push(temp[0] + temp[1])
arr[temp[0]] = null;
arr[temp[1]] = null;
i = 0;
}
console.log(arr)
console.log('res is ' + res)
}
return res.reduce(function (a, b) {
return a + b;
})
}
pairwise([], 100)