Created
January 22, 2019 17:52
-
-
Save condef5/66d320197e95c9a2ddcc33f2c7fa866a to your computer and use it in GitHub Desktop.
Codewars for ever
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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