Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //17 EXERCISE: ARRAYS ADVANCED/09. Gladiator Inventory.js
- gladiatorInventory = arr => {
- 'use strict'
- let workArr = arr
- let account = workArr.shift()
- let len = workArr.length
- let command = workArr.shift()
- //while (command !== undefined) {
- for (let i = 0; i < len; i++) {
- if (command.includes('Buy')) {
- command = command.replace('Buy ', '')
- if (!account.includes(command))
- account = account.concat(' ' + command)
- }
- if (command.includes('Trash ')) {
- command = command.replace('Trash ', '')
- if (account.includes(command))
- account = account.replace(command + ' ', '')
- }
- if (command.includes('Repair')) {
- command = command.replace('Repair ', '')
- if (account.includes(command)) {
- account = account.replace(command + ' ', '')
- account = account.concat(' ' + command)
- }
- }
- if (command.includes('Upgrade')) {
- command = command.replace('Upgrade ', '')
- let game = ''
- for (let i = 0; i < command.length; i++) {
- if (command[i] === '-') { //трие от „-“ до края
- game = command.replace(command.substring(i, command.length), '')
- }
- }
- if (account.includes(game)) {
- command = command.replace('-', ':')
- account = account.replace(game, game + ' ' + command)
- }
- }
- command = workArr.shift()
- }
- console.log(account)
- }
- gladiatorInventory0 = input => {
- 'use strict'
- let games = input.shift().split(' ');
- //let stop = false;
- let index;
- for (let i = 0; i < input.length; i++) {
- let command = input[i].split(' ');
- switch (command[0]) {
- /* case undefined:
- console.log(games.join(' '));
- stop = true;
- break;
- */
- case 'Buy':
- index = games.indexOf(command[1]);
- if (index === -1) {
- games.push(command[1]);
- }
- break;
- case 'Trash':
- index = games.indexOf(command[1]);
- if (index !== -1) {
- games.splice(index, 1);
- }
- break;
- case 'Repair':
- index = games.indexOf(command[1]);
- if (index !== -1) {
- let updated = games.splice(index, 1);
- games.push(updated[0]);
- }
- break;
- case 'Upgrade':
- let expansion = command[1].split('-');
- index = games.indexOf(expansion[0]);
- if (index !== -1) {
- games.splice(index + 1, 0, `${expansion[0]}:${expansion[1]}`);
- }
- break;
- }
- /* if (stop) {
- break;
- } */
- }
- console.log(games.join(' '));
- }
- gladiatorInventory0(
- ['SWORD Shield Spear',
- 'Buy Bag',
- 'Trash Shield',
- 'Repair Spear',
- 'Upgrade SWORD-Steel']
- )
- gladiatorInventory0(
- ['SWORD Shield Spear',
- 'Trash Bow',
- 'Repair Shield',
- 'Upgrade Helmet-V']
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement