Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function movingTrgt(data) {
- let targets = data.shift().split(" ");
- targets = targets.map(Number);
- let index = 0;
- let command = data[index];
- index++;
- while (command !== "End") {
- let tokens = command.split(" ");
- let currentIndex = +tokens[1];
- let currentProperty = +tokens[2];
- switch (tokens[0]) {
- case "Shoot":
- if (targets[currentIndex] !== undefined) {
- if (targets[currentIndex] - currentProperty <= 0) {
- targets.splice(currentIndex, 1);
- } else {
- targets.splice(
- currentIndex,
- 1,
- targets[currentIndex] - currentProperty
- );
- }
- }
- break;
- case "Add":
- if (targets[currentIndex] !== undefined) {
- targets.splice(currentIndex, 0, currentProperty);
- } else {
- console.log("Invalid placement!");
- }
- break;
- case "Strike":
- if (targets[currentIndex] !== undefined) {
- if (
- targets[currentIndex - currentProperty] !== undefined &&
- targets[currentIndex + currentProperty] !== undefined
- ) {
- let elementsToRemove = 2 * currentProperty + 1;
- let startingIndexToRemove =
- currentIndex - currentProperty;
- targets.splice(startingIndexToRemove, elementsToRemove);
- } else {
- console.log("Strike missed!");
- }
- }
- break;
- }
- command = data[index];
- index++;
- }
- console.log(targets.join("|"));
- }
Advertisement
Add Comment
Please, Sign In to add comment