dabidabidesh

Gladiator Inventory

Jun 17th, 2020
367
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //17 EXERCISE: ARRAYS ADVANCED/09. Gladiator Inventory.js
  2. gladiatorInventory = arr => {
  3.   'use strict'
  4.  
  5.   let workArr = arr
  6.   let account = workArr.shift()
  7.  
  8.   let len = workArr.length
  9.   let command = workArr.shift()
  10.   //while (command !== undefined) {
  11.   for (let i = 0; i < len; i++) {
  12.  
  13.     if (command.includes('Buy')) {
  14.       command = command.replace('Buy ', '')
  15.       if (!account.includes(command))
  16.         account = account.concat(' ' + command)
  17.     }
  18.     if (command.includes('Trash ')) {
  19.       command = command.replace('Trash ', '')
  20.       if (account.includes(command))
  21.         account = account.replace(command + ' ', '')
  22.     }
  23.     if (command.includes('Repair')) {
  24.       command = command.replace('Repair ', '')
  25.       if (account.includes(command)) {
  26.         account = account.replace(command + ' ', '')
  27.         account = account.concat(' ' + command)
  28.       }
  29.     }
  30.     if (command.includes('Upgrade')) {
  31.       command = command.replace('Upgrade ', '')
  32.       let game = ''
  33.       for (let i = 0; i < command.length; i++) {
  34.         if (command[i] === '-') {  //трие от „-“ до края
  35.           game = command.replace(command.substring(i, command.length), '')
  36.         }
  37.       }
  38.       if (account.includes(game)) {
  39.         command = command.replace('-', ':')
  40.         account = account.replace(game, game + ' ' + command)
  41.       }
  42.     }
  43.     command = workArr.shift()
  44.   }
  45.   console.log(account)
  46. }
  47.  
  48. gladiatorInventory0 = input => {
  49.   'use strict'
  50.  
  51.   let games = input.shift().split(' ');
  52.   //let stop = false;
  53.   let index;
  54.  
  55.   for (let i = 0; i < input.length; i++) {
  56.     let command = input[i].split(' ');
  57.  
  58.     switch (command[0]) {
  59.       /* case undefined:
  60.         console.log(games.join(' '));
  61.         stop = true;
  62.         break;
  63.  */
  64.       case 'Buy':
  65.         index = games.indexOf(command[1]);
  66.  
  67.         if (index === -1) {
  68.           games.push(command[1]);
  69.         }
  70.         break;
  71.  
  72.       case 'Trash':
  73.         index = games.indexOf(command[1]);
  74.  
  75.         if (index !== -1) {
  76.           games.splice(index, 1);
  77.         }
  78.         break;
  79.  
  80.       case 'Repair':
  81.         index = games.indexOf(command[1]);
  82.  
  83.         if (index !== -1) {
  84.           let updated = games.splice(index, 1);
  85.           games.push(updated[0]);
  86.         }
  87.         break;
  88.  
  89.       case 'Upgrade':
  90.  
  91.         let expansion = command[1].split('-');
  92.         index = games.indexOf(expansion[0]);
  93.  
  94.         if (index !== -1) {
  95.           games.splice(index + 1, 0, `${expansion[0]}:${expansion[1]}`);
  96.         }
  97.         break;
  98.     }
  99.  
  100.     /* if (stop) {
  101.       break;
  102.     } */
  103.   }
  104.   console.log(games.join(' '));
  105. }
  106.  
  107. gladiatorInventory0(
  108.   ['SWORD Shield Spear',
  109.     'Buy Bag',
  110.     'Trash Shield',
  111.     'Repair Spear',
  112.     'Upgrade SWORD-Steel']
  113. )
  114. gladiatorInventory0(
  115.   ['SWORD Shield Spear',
  116.     'Trash Bow',
  117.     'Repair Shield',
  118.     'Upgrade Helmet-V']
  119. )
RAW Paste Data