Facebook
From Bistre Moth, 6 Months ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 64
  1. function demo(arr) {
  2.   let array = arr;
  3.  
  4.   let equimpents = array.slice(0, 1);
  5.  
  6.   let arrOfEquipments = equimpents.toString().split(" ");
  7.  
  8.   let newArrOfCommands = array.splice(1);
  9.  
  10.   let lengthOfCommands = newArrOfCommands.length;
  11.  
  12.   let lengthOfEquipments = arrOfEquipments.length;
  13.  
  14.   for (let i = 0; i < lengthOfCommands; i++) {
  15.     let currentElement = "";
  16.  
  17.     currentElement = newArrOfCommands.splice(0, 1).toString().split(" ");
  18.  
  19.     let command = "";
  20.  
  21.     command = currentElement[0];
  22.  
  23.     lengthOfEquipments = arrOfEquipments.length;
  24.  
  25.     if (command == "Buy") {
  26.       let product = "";
  27.  
  28.       product = currentElement[1];
  29.  
  30.       arrOfEquipments.push(product);
  31.     } else if (command == "Trash") {
  32.       let productToDelete = "";
  33.  
  34.       productToDelete = currentElement[1];
  35.  
  36.       for (let j = 0; j < lengthOfEquipments; j++) {
  37.         let weapon = "";
  38.  
  39.         weapon = arrOfEquipments[j];
  40.  
  41.         if (weapon == productToDelete) {
  42.           arrOfEquipments.splice(j, 1);
  43.         }
  44.       }
  45.     } else if (command == "Repair") {
  46.       let weaponForRepair = "";
  47.  
  48.       weaponForRepair = currentElement[1];
  49.  
  50.       for (let k = 0; k < lengthOfEquipments; k++) {
  51.         let weapon = "";
  52.  
  53.         weapon = arrOfEquipments[k];
  54.  
  55.         if (weapon == weaponForRepair) {
  56.           let elementForRemove = arrOfEquipments.splice(k, 1).toString();
  57.  
  58.           arrOfEquipments.push(elementForRemove);
  59.         }
  60.       }
  61.     } else if (command == "Upgrade") {
  62.       let weapon = currentElement[1].toString();
  63.  
  64.       let currentWeapon = "";
  65.  
  66.       let upgrade = "";
  67.  
  68.       for (let y = 0; y < weapon.length - 1; y++) {
  69.         let letter = "";
  70.  
  71.         letter = weapon[y];
  72.  
  73.         if (letter == "-") {
  74.           for (let a = y + 1; a <= weapon.length - 1; a++) {
  75.             let lettersForUpgrade = "";
  76.             lettersForUpgrade = weapon[a];
  77.             upgrade += lettersForUpgrade;
  78.             y = a;
  79.           }
  80.         } else {
  81.           currentWeapon += letter;
  82.         }
  83.       }
  84.  
  85.       for (let r = 0; r < lengthOfEquipments; r++) {
  86.         let typeOfWeapon = "";
  87.         typeOfWeapon = arrOfEquipments[r];
  88.  
  89.         if (typeOfWeapon == upgrade) {
  90.           arrOfEquipments.splice(r + 1, 0, `${currentWeapon}:${upgrade}`);
  91.         }
  92.       }
  93.     }
  94.   }
  95.   console.log(arrOfEquipments.join(" "));
  96. }