AreWe

Tanks Collector 100/100

Jun 27th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tankCollector = (input = []) => {
  2.     let tanks = input.shift().split(', ');  
  3.     let cmds = Number(input.shift());
  4.  
  5.     for(let i = 0; i < cmds; i++) {
  6.         let cmd = input.shift().split(', ');
  7.  
  8.         if(cmd[0] === 'Add') {
  9.             if(tanks.indexOf(cmd[1]) !== -1) {
  10.                 console.log('Tank is already bought');
  11.             } else {
  12.                 tanks.push(cmd[1]);
  13.                 console.log(`Tank successfully bought`);
  14.             }
  15.         } else if(cmd[0] === 'Remove') {
  16.             let index = tanks.indexOf(cmd[1]);
  17.             if(index !== -1) {
  18.                 tanks.splice(index, 1);
  19.                 console.log('Tank successfully sold');
  20.             } else {
  21.                 console.log('Tank not found');
  22.             }
  23.         } else if(cmd[0] === 'Remove At') {
  24.             let index = Number(cmd[1]);
  25.             if(tanks[cmd[1]]) {
  26.                 tanks.splice(index, 1);
  27.                 console.log('Tank successfully sold');
  28.             } else {
  29.                 console.log('Index out of range');
  30.             }
  31.         } else if(cmd[0] === 'Insert') {
  32.             let index = Number(cmd[1]), name = cmd[2];
  33.             if(!tanks[index]) {
  34.                 console.log('Index out of range');
  35.                 continue;
  36.             }
  37.  
  38.             let foundIndex = tanks.indexOf(name);
  39.             if(foundIndex !== -1) {
  40.                 console.log('Tank is already bought');
  41.                 continue;
  42.             }
  43.  
  44.             tanks.splice(index, 0, name);
  45.             console.log('Tank successfully bought');
  46.         }
  47.     }
  48.  
  49.     console.log(tanks.join(', '));
  50. };
Add Comment
Please, Sign In to add comment