Created
June 4, 2020 13:03
-
-
Save blacksheep557/75c8c121176df378a5f4285f707c3e3b to your computer and use it in GitHub Desktop.
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
| function asteroidCollision(asteroids) { | |
| while (asteroids.length > 1) { | |
| for (let i = 1; i < asteroids.length; i++) { | |
| let [asteroidX, asteroidY] = [asteroids[i - 1], asteroids[i]]; | |
| while (asteroidX > 0 && asteroidY < 0&&i>=1) { | |
| if (Math.abs(asteroidX) === Math.abs(asteroidY)) { | |
| asteroids.splice(i - 1, 2) | |
| } else if (Math.abs(asteroidX) < Math.abs(asteroidY)) { | |
| asteroids.splice(i - 1, 1) | |
| } else { | |
| asteroids.splice(i, 1) | |
| } | |
| i = i - 1; | |
| [asteroidX, asteroidY] = [asteroids[i - 1], asteroids[i]]; | |
| } | |
| if (i === asteroids.length - 1) { | |
| return asteroids | |
| } | |
| } | |
| } | |
| return asteroids | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment