0

I have two arrays of numbers. First one contains N numbers, imagine it's a field. The second one contains 2 elements, first is a special number (bomb number), the second one is the power of the bomb number. My task is to remove n numbers (where n = power of the bomb number) before and after the bomb number and the bomb number itself.

What's the best approach to take?

I found the index of the bomb number using .indexOf() and .splice() to remove the elements before the occurrences of the bomb number, but after that all indexes are shifted and that's broke my logic.

This is my logic at this point:

function bombNumbers(numbersSequance, bombNumberInfo) {
    const bombNumber = bombNumberInfo[0];
    const bombNumberPower = bombNumberInfo[1];

    while (numbersSequance.includes(bombNumber)) {
        let bombNumberIndex = numbersSequance.indexOf(bombNumber);
        let startingIndex = bombNumberIndex - bombNumberPower;
        let endIndex = bombNumberIndex + bombNumberPower;
        numbersSequance.splice(startingIndex, bombNumberPower);
    }
}

bombNumbers([1, 2, 2, 4, 2, 2, 2, 9],
    [4, 2]);

0 Answers0