Advertisement
vladovip

ShootForTheWin_JS Fund Prep Exam

Feb 20th, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shootForTheWin(input) {
  2.   let shootedTargetsList = input[0].split(" ").map(Number);
  3.   // console.log(shootedTargetsList);
  4.   let commandsIndices = input.shift();
  5.   //console.log(commandsIndices);
  6.   let countOfShootedTargets = 0;
  7.  
  8.  
  9.   while (commandsIndices !== "End") {
  10.     let operationalIndex = Number(commandsIndices);
  11.  
  12.     if (operationalIndex >= 0 && operationalIndex < shootedTargetsList.length) {
  13.       for (let index = 0; index < shootedTargetsList.length; index++) {
  14.         let currentTarget = shootedTargetsList[operationalIndex];
  15.         if (operationalIndex !== index && shootedTargetsList[index] !== -1) {
  16.           if (shootedTargetsList[index] > currentTarget) {
  17.             shootedTargetsList[index] -= currentTarget;
  18.           } else {
  19.             shootedTargetsList[index] += currentTarget;
  20.           }
  21.         }
  22.       }
  23.       shootedTargetsList[operationalIndex] = -1;
  24.       countOfShootedTargets++;
  25.     }
  26.     commandsIndices = input.shift();
  27.   }
  28.  
  29.   console.log(
  30.     `Shot targets: ${countOfShootedTargets} -> ${shootedTargetsList.join(" ")}`
  31.   );
  32. }
  33.  
  34. shootForTheWin(["24 50 36 70", "0", "4", "3", "1", "End"]);
  35. shootForTheWin(["30 30 12 60 54 66", "5", "2", "4", "0", "End"]);
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement