-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
47 lines (43 loc) · 1.05 KB
/
test.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
const array = ["one", "two", "three", "four", "five", "six"];
const elements = [];
const distance = 10;
if(array.length % 2 === 1) {
const base = Math.round(array.length / 2);
let step = base - 1;
array.forEach((item, index) => {
if(index + 1 < base) {
elements.push(step * distance);
step--;
}
else if(index + 1 > base) {
elements.push(step * distance);
step++;
}
else {
elements.push(0);
step++;
}
})
}
else {
const base1 = Math.round(array.length / 2);
const base2 = base1 + 1;
let step = base1 - 1;
array.forEach((item, index) => {
if(index + 1 < base1) {
elements.push(step * distance);
step--;
}
else if(index + 1 > base2) {
elements.push(step * distance);
step++;
}
else {
elements.push(0);
if(index + 1 === base2) {
step++;
}
}
})
}
console.log(elements);