AlexanderHristov

9 *Gladiator Inventory - JS Array Advanced

Nov 3rd, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) {
  2.  
  3.     let inventoryArr = inputArr.shift();
  4.     inventoryArr = inventoryArr.split(' ');
  5.  
  6.     for (let currentArr of inputArr) {
  7.         let currentCommand = currentArr.split(' ');
  8.         let weapon = currentCommand[1];
  9.  
  10.         if (currentCommand.includes('Buy')) {
  11.             if (!inventoryArr.includes(weapon)) {
  12.                 inventoryArr.push(weapon);
  13.             }
  14.         } else if (currentCommand.includes('Trash')) {
  15.             if (inventoryArr.includes(weapon)) {
  16.                 inventoryArr.splice(inventoryArr.indexOf(weapon), 1);
  17.             }
  18.         } else if (currentCommand.includes('Repair')) {
  19.             if (inventoryArr.includes(weapon)) {
  20.                 inventoryArr.push(weapon);
  21.                 inventoryArr.splice(inventoryArr.indexOf(weapon), 1);
  22.             }
  23.         } else if (currentCommand.includes('Upgrade')) {
  24.             let weaponUpgrade = weapon.replace('-',':');
  25.             weapon = weapon.split('-');
  26.             let upgrade = weapon[0];
  27.             if (inventoryArr.includes(upgrade)) {
  28.                 inventoryArr.splice(inventoryArr.indexOf(upgrade)+1, 0, weaponUpgrade);
  29.             }
  30.         }
  31.     }
  32.  
  33.     console.log(inventoryArr.join(' '));
  34. }
Advertisement
Add Comment
Please, Sign In to add comment