Liliana797979

viarno reshenie shoot the win - mid exam - fundamentals

Jul 16th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function shootForTheWin(input) {
  3.     let targets = input.shift().split(" ").map(Number);
  4.  
  5.     let count = 0;
  6.     let command = input.shift();
  7.  
  8.     while (command !== "End") {
  9.         let indexOfTheTarget = Number.parseInt(command);
  10.  
  11.         if (indexOfTheTarget >= 0 && indexOfTheTarget < targets.length) {
  12.             for (let i = 0; i < targets.length; i++) {
  13.                 let currentTarget = targets[indexOfTheTarget];
  14.        
  15.                 if (i !== indexOfTheTarget && targets[i] !== -1) {
  16.                     if (targets[i] > currentTarget) {
  17.                         targets[i] -= currentTarget;
  18.                     } else {
  19.                         targets[i] += currentTarget;
  20.                     }
  21.                 }
  22.             }
  23.  
  24.             targets[indexOfTheTarget] = -1;
  25.             count++;
  26.         }
  27.  
  28.         command = input.shift();
  29.     }
  30.  
  31.     console.log(`Shot targets: ${count} -> ${targets.join(" ")}`);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment