Skip to content

Instantly share code, notes, and snippets.

@condef5
Created January 22, 2019 17:52
Show Gist options
  • Select an option

  • Save condef5/66d320197e95c9a2ddcc33f2c7fa866a to your computer and use it in GitHub Desktop.

Select an option

Save condef5/66d320197e95c9a2ddcc33f2c7fa866a to your computer and use it in GitHub Desktop.
Codewars for ever
// solution initial
function dirReduc(arr) {
while (true) {
for (let i = 0; i < arr.length - 1; i++) {
if (
(arr[i] === 'NORTH' && arr[i + 1] === 'SOUTH') ||
(arr[i] === 'SOUTH' && arr[i + 1] === 'NORTH') ||
(arr[i] === 'EAST' && arr[i + 1] === 'WEST') ||
(arr[i] === 'WEST' && arr[i + 1] === 'EAST')
) {
arr[i] = 0;
arr[i + 1] = 0;
}
}
if (arr.indexOf(0) === -1) break;
arr = arr.filter((item) => item !== 0);
}
return arr;
}
// dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]) => ["WEST"]
// dirReduc(["NORTH", "WEST", "SOUTH", "EAST"]) => ["NORTH", "WEST", "SOUTH", "EAST"]
// dirReduc(["NORTH", "SOUTH", "EAST", "WEST", "EAST", "WEST"]) => []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment