JSFiddle - React, Tailwind, and code Playground
by ubershmekel
JavaScript
function asteroids(arr) {
// delete negatives when iterating left to right
// then delete positives when iterating right to left
return [];
}
function assert(val) {
if (!val) {
const err = new Error("Failed test!")
console.log("Error trace", err.stack)
throw err;
}
}
function equal(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
function runTests() {
// The 8 and -5 collide, 8 wins. The 5 and 8 never collide.
assert(equal(asteroids([5, 8, -5]), [5, 8]))
// The 10 and -10 collide and they both explode.
assert(asteroids([10, -10]), [])
}
runTests();