Advertisement
isebs

Untitled

Feb 28th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function tanksColector(input) {
  2.   let list = input.shift().split(", ");
  3.   let numOfCom = +input.shift();
  4.  
  5.   const add = (tankName) => {
  6.       if (list.includes(tankName)) {
  7.           console.log(`Tank is already bought`);
  8.       } else {
  9.           console.log(`Tank successfully bought`);
  10.           list.push(tankName);
  11.       }
  12.   }
  13.  
  14.   const remove = (tankName) => {
  15.       if (list.includes(tankName)) {
  16.           console.log(`Tank successfully sold`);
  17.           let index = list.indexOf(tankName);
  18.           list.splice(index, 1);
  19.       } else {
  20.           console.log(`Tank not found`);
  21.       }
  22.   }
  23.   const removeAt = (index) => {
  24.       index = Number(index);
  25.       if (index >= 0 && index < list.length) {
  26.           console.log(`Tank successfully sold`);
  27.           list.splice(index, 1);
  28.       } else {
  29.           console.log(`Index out of range`);
  30.       }
  31.   }
  32.   const insert = (index, tankName) => {
  33.       index = Number(index);
  34.       if (index >= 0 && index < list.length && list.includes(tankName)) {
  35.           console.log(`Tank is already bought`);
  36.       } else if (index >= 0 && index < list.length && !list.includes(tankName)) {
  37.           console.log(`Tank successfully bought`);
  38.           list.splice(index, 0, tankName);
  39.       } else {
  40.           console.log(`Index out of range`);
  41.       }
  42.   }
  43.   for (let i = 0; i < numOfCom; i++) {
  44.       let instructions = input[i].split(", ");
  45.       let command = instructions[0];
  46.       let el1 = instructions[1];
  47.       let el2 = instructions[2];
  48.  
  49.       if (command === "Add") {
  50.           add(el1);
  51.       } else if (command === "Insert") {
  52.           insert(el1, el2);
  53.       } else if (command === "Remove") {
  54.           remove(el1);
  55.       } else if (command === "Remove At") {
  56.           removeAt(el1);
  57.       }
  58.   }
  59.   console.log(list.join(", "));
  60. }
  61. tanksColector([
  62.     'T-34-85 Rudy, SU-100Y, STG',
  63.     '3',
  64.     'Add, King Tiger(C)',
  65.     'Insert, 2, IS-2M',
  66.     'Remove, T-34-85 Rudy'
  67.   ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement