AlexanderHristov

6. Bomb Numbers (JS Array Advanced)

Oct 31st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArrTargets, inputArrBomb) {
  2.  
  3.     let [bomb, blast] = inputArrBomb;
  4.     let targets = inputArrTargets;
  5.     let sum = targets.reduce((a, b) => a + b);
  6.  
  7.     if (blast < 0) {
  8.         blast = 0;
  9.     }
  10.  
  11.     for (i = 0; i < targets.length; i++) {
  12.  
  13.         if (targets[i] === bomb && i - blast < 0) {
  14.             if (blast > targets.length) {
  15.                 sum = 0;
  16.             } else {
  17.                 sum = 0;
  18.                 targets.splice(0, (blast * 2 + 1) + (i - blast));
  19.                 for (j = 0; j < targets.length; j++) {
  20.                     sum += targets[j];
  21.                 }
  22.                 i = 0;
  23.             }
  24.  
  25.         } else if (targets[i] === bomb && i + blast < targets.length) {
  26.             if (blast > targets.length) {
  27.                 sum = 0;
  28.             } else {
  29.                 sum = 0;
  30.                 targets.splice(i - blast, blast * 2 + 1);
  31.                 for (j = 0; j < targets.length; j++) {
  32.                     sum += targets[j];
  33.                 }
  34.                 i = 0;
  35.             }
  36.  
  37.         } else if (targets[i] === bomb && i + blast >= targets.length) {
  38.             if (blast > targets.length) {
  39.                 sum = 0;
  40.             } else {
  41.                 sum = 0;
  42.                 targets.splice(i - blast);
  43.                 for (j = 0; j < targets.length; j++) {
  44.                     sum += targets[j];
  45.                 }
  46.                 i = 0;
  47.             }
  48.         }
  49.     }
  50.  
  51.     console.log(sum);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment