Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created June 4, 2020 13:02
Show Gist options
  • Select an option

  • Save blacksheep557/b0f2fb70e88c0c1daf3092e802f073e7 to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/b0f2fb70e88c0c1daf3092e802f073e7 to your computer and use it in GitHub Desktop.
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
}
@McLarenCollege
Copy link

No this is not it.
You now have 3 nested loops while the original code had only 2.

Try again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment