Nachino
Greenunknown
javascript
2 years ago
669 B
2
Indexable
Never
const meteors = [8,3,-5, -2, 7, 9, -2, -30, 7, 9, 8, -1, -4, 10, -20] // (8, -5, 7, -30, -1, -20) -> (8, -30, -1, -20) -> (-30, -1, -20) const recursiveMeteorCrashDetector = (meteors) => { let collisions = false meteors.reduce((acc, curr) => { if(acc.length > 0 && curr*acc.slice(-1) < 0) { const difference = Math.abs(acc.slice(-1)) - Math.abs(curr) if(Math.abs(acc.slice(-1)) - Math.abs(curr) < 0) { collisions = true acc.splice(-1, 1) } return acc } return [...acc, curr] }, []) if(collisions) { return recursiveMeteorCrashDetector(meteors) } } console.log(recursiveMeteorCrashDetector(recursiveMeteorCrashDetector(meteors)))