Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []){
  2.  
  3.     let tanks = input.shift().split(`, `);
  4.     let commandsCount = input.shift();
  5.    
  6.     for(let i = 0; i < commandsCount; i++){
  7.        
  8.         let tokens = input.shift().split(`, `);
  9.         let action = tokens[0];
  10.         let nameOrIndex = tokens[1];
  11.         let secondName = tokens[2];
  12.  
  13.         if(action === `Add`){
  14.             let index = tanks.indexOf(nameOrIndex);
  15.             if(index > -1){
  16.                 console.log(`Tank is already bought`);
  17.             }
  18.             else{
  19.                 tanks.push(nameOrIndex);
  20.                 console.log(`Tank successfully bought`);
  21.             }
  22.         }
  23.         else if(action === `Insert`){
  24.             let index = +nameOrIndex;
  25.             if(index > 0 && index > tanks.length){
  26.                 console.log(`Index out of range`);
  27.             }
  28.             else{
  29.                 let indexOfSecond = tanks.indexOf(secondName);
  30.                 if(index > -1){
  31.                     tanks.splice(index, 0, secondName)
  32.                     console.log(`Tank successfully bought`);
  33.                 }
  34.                 else{
  35.                     console.log(`Tank is already bought`);
  36.                 }
  37.             }
  38.         }
  39.         else if(action === `Remove`){
  40.             let index = tanks.indexOf(nameOrIndex);
  41.             if(index > -1){
  42.                 tanks.splice(index, 1)
  43.                 console.log(`Tank successfully sold`);
  44.             }
  45.             else{
  46.                 console.log(`Tank not found`);
  47.             }
  48.         }
  49.         else if(action === `Remove At`){
  50.             let index = +nameOrIndex;
  51.             if(index > 0 && index > tanks.length){
  52.                 console.log(`Index out of range`);
  53.             }
  54.             else{
  55.                 let index = +nameOrIndex;
  56.                 tanks.splice(index, 1)
  57.                 console.log(`Tank successfully sold`);
  58.             }
  59.         }
  60.     }
  61.     console.log(tanks.join(`, `))
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement