-
Notifications
You must be signed in to change notification settings - Fork 0
/
pract0618.js
73 lines (64 loc) · 1.45 KB
/
pract0618.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* const generateArray = (a) => {
let tomb = [1];
for (let i = 0; i < a; i++) {
tomb[i] = Math.floor(Math.random() * 10) + 1;
}
return tomb;
};
*/
const smallestDifference = (arr1, arr2) => {
let diff2 = 20;
let temp1;
let temp2;
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if ((diff2 > arr1[i] - arr2[j] && arr1[i] - arr2[j] >= 0) || (diff2 > arr2[j] - arr1[i] && arr2[j] - arr1[i] >= 0)) {
diff2 = arr1[i] - arr2[j];
temp1 = arr1[i];
temp2 = arr2[j];
}
}
}
return [temp1, temp2];
};
const stepSum = (arr) => {
if (arr.length === 1) {
console.log(arr);
return arr;
} else {
let tomb = [];
for (let i = 0; i < arr.length; i++) {
if (i > 0) {
tomb.push(arr[i] + arr[i - 1]);
}
}
tomb = stepSum(tomb);
console.log(arr);
}
};
/* const permutations = (arr) => {
console.log(arr);
// if ()
for (let i = 0; i < arr.length; i++) {
let tomb = arr;
let temp;
if (i > 0 && arr[i] !== tomb[i + 1]) {
temp = tomb[i + 1];
tomb[i + 1] = arr[i];
arr[i] = temp;
return tomb;
}
tomb = permutations(tomb);
}
return arr;
}; */
/* let arr1 = [10, 3, 2, 1];
console.log(arr1);
let arr2 = [6, 8, 9, 10];
console.log(arr2);
console.log(smallestDifference(arr1, arr2));
*/
let array = [1, 2, 3];
// console.log(permutations(array));
stepSum(array);
// console.log(permutations(array));