Skip to content

Instantly share code, notes, and snippets.

@alexismellamo
Created April 11, 2019 00:13
Show Gist options
  • Save alexismellamo/a23956174375e3c82a82252e28d56823 to your computer and use it in GitHub Desktop.
Save alexismellamo/a23956174375e3c82a82252e28d56823 to your computer and use it in GitHub Desktop.
const opposite = {
'NORTH': 'SOUTH',
'SOUTH': 'NORTH',
'EAST': 'WEST',
'WEST': 'EAST'
};
function dirReduc(arr) {
let hasReduced = false
let newArray = []
arr.forEach((actual, i, a) => {
if(hasReduced) return
const next = a[i+1];
if(!next) return
if(opposite[actual] === next) {
hasReduced = true
newArray = [...a.slice(0, i), ...a.slice(i+2)]
}
})
if(hasReduced) {
return dirReduc(newArray)
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment