-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (55 loc) · 1.55 KB
/
index.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
module.exports = function (arr) {
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function newCenterValue (a3) {
if (arraysEqual(a3, [1,1,1]) ) {
return 0;
}
else if (arraysEqual(a3, [1,1,0]) ) {
return 0;
}
else if (arraysEqual(a3, [1,0,1]) ) {
return 0;
}
else if (arraysEqual(a3, [1,0,0]) ) {
return 1;
}
else if (arraysEqual(a3, [0,1,1]) ) {
return 1;
}
else if (arraysEqual(a3, [0,1,0]) ) {
return 1;
}
else if (arraysEqual(a3, [0,0,1]) ) {
return 1;
}
else if (arraysEqual(a3, [0,0,0]) ) {
return 0;
} else {
return null;
}
}
var oldArr = arr.slice(0);
var newArr = arr.slice(0);
// before 111 110 101 100 011 010 001 000
// after 0 0 0 1 1 1 1 0
oldArr.unshift(0);
oldArr.push(0);
newArr.unshift(0);
newArr.push(0);
for (var i = 1; i < oldArr.length - 2; i++) {
if (oldArr[i] !== 0 && oldArr[i] !== 1) {
console.warn('Array element must be 1 or 0', typeof oldArr[i])
return null;
}
newArr[i] = newCenterValue(oldArr.slice(i-1, i+2));
}
return newArr;
}