Given an array of words, I want to identify pairs of opposite directions and remove them from the array, returning a new modified array.
For instance:
let words = ["up", "right", "left", "down", "left", "down", "up", "left"]
should result in:
newWords = ["left", "left"]
Here's my current approach. However, I am encountering an issue where it gives undefined
.
let words = ["up", "right", "left", "down", "left", "down", "up", "left"]
function checkOpposite(pair) {
let upDown = ["up", "down"]
let downUp = ["down", "up"]
let rightLeft = ["right", "left"]
let leftRight = ["left", "right"]
for (var i = 0; i < pair.length - 1; i++) {
if (pair[i] + pair[i + 1] === upDown)
if (pair[i] + pair[i + 1] === downUp)
if (pair[i] + pair[i + 1] === rightLeft)
if (pair[i] + pair[i + 1] === leftRight) {
return true
}
}
}
function filterDirections(words) {
words.filter(checkOpposite)
}