I have a problem where I am supposed to remove redundant directions from array. For example, if I have ["N", "S", "E", "W", "E"], I would return ["E"] because north and south cancel out and east and west cancel out to leave the one east. If they are adjacent, they cancel out. If I have ['W', 'N', 'S', 'E', 'N'], it returns ["N"] because N,S cancel, leaving W, E which also cancels. I created a function that works:
function mapQuest(arr) {
const directions = { N: 2, S: -2, W: 1, E: -1 };
for (let i = 0; i < arr.length; i++) {
while (directions[arr[i]] + directions[arr[i + 1]] == 0) {
arr.splice(i, 2);
i = 0;
}
}
return arr;
}
But the problem is asking to solve the problem using reduce method in javascript. I started to write one:
function mapQuest(arr) {
let result = arr.reduce((acc, dir) => acc[dir]++, { N: 0, S: 0, W: 0, E: 0 });
}
But I don't know where to go from here and how to cancel out only adjacent directions without a for loop iterating through i. If someone could help out, that would be great. Thanks!