-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directions Reduction.js
78 lines (67 loc) · 2.34 KB
/
Directions Reduction.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
74
75
76
77
78
function dirReduce(arr){
var resultArr = arr;
var reducedArr = [];
function reduceArr(array) {
array.forEach((value, index) => {
switch(value) {
case 'NORTH':
if (array[index + 1] === 'SOUTH'){
array[index] = ''; array[index + 1] = '';
}
break;
case 'SOUTH':
if (array[index + 1] === 'NORTH'){
array[index] = ''; array[index + 1] = '';
}
break;
case 'EAST':
if (array[index + 1] === 'WEST'){
array[index] = ''; array[index + 1] = '';
}
break;
case 'WEST':
if (array[index + 1] === 'EAST'){
array[index] = ''; array[index + 1] = '';
}
break;
case '':
break;
}
})
return array.filter(el => el !== '');
}
// function reduceArr(array) {
// array.forEach((value, index) => {
// switch(value) {
// case 'NORTH':
// if (array[index + 1] === 'SOUTH'){
// array.splice(index, index + 2);
// }
// break;
// case 'SOUTH':
// if (array[index + 1] === 'NORTH'){
// array.splice(index, index + 2);
// }
// break;
// case 'EAST':
// if (array[index + 1] !== 'WEST'){
// array.splice(index, index + 2);
// }
// break;
// case 'WEST':
// if (array[index + 1] !== 'EAST'){
// array.splice(index, index + 2);
// }
// break;
// }
// })
// return array;
// }
while(resultArr.length !== reducedArr.length){
console.log(reducedArr);
resultArr = reduceArr(resultArr);
reducedArr = reduceArr(resultArr);
}
return resultArr;
}
console.log(dirReduce(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]));