Created
June 4, 2020 13:02
-
-
Save blacksheep557/b0f2fb70e88c0c1daf3092e802f073e7 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
No this is not it.
You now have 3 nested loops while the original code had only 2.
Try again.