nikolayneykov

Untitled

Feb 23rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.  
  3.     let peshoInventory = arr.shift().split(' ');
  4.     for (let param of arr) {
  5.         let currentElement = param.split(/[-\s+]/);
  6.         let command = currentElement[0];
  7.      
  8.         let equipment = currentElement[1]; //.split("-")[0];
  9.         let index = peshoInventory.indexOf(equipment);
  10.  
  11.         if (command === "Buy" && index === -1) {
  12.             peshoInventory.push(equipment);
  13.         } else if (command === "Trash" && index !== -1) {
  14.             peshoInventory.splice(index, 1)
  15.         } else if (command === "Repair" && index !== -1) {
  16.             let forRepair = peshoInventory.splice(index, 1);
  17.             peshoInventory.push(forRepair);
  18.         } else if (command === "Upgrade" && index !== -1) {
  19.             let expansion = equipment+':'+currentElement[2];
  20.             peshoInventory.splice(index + 1, 0, expansion);
  21.         }
  22.  
  23.     }
  24.     console.log(peshoInventory.join(" "))
  25. }
Advertisement
Add Comment
Please, Sign In to add comment