Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr1, arr2) {
- let bomb = arr2[0];
- let power = arr2[1];
- for (let i = 0; i < arr1.length; i++) {
- if (arr1[i] === bomb) {
- let startIndex = Math.max(0, i - power); //to ensure detonation will not go outside the array with negative index
- let endIndex = Math.min(arr1.length - 1, i + power); //to ensure detonation will not go outside the array with bigger index than array max
- let bombCount = endIndex - startIndex + 1;
- arr1.splice(startIndex, bombCount);
- i = startIndex - 1; //get to the current element new index
- }
- }
- let sum = 0;
- for (let k = 0; k < arr1.length; k++) {
- sum += arr1[k];
- }
- console.log(sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement