Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []){
- let tanks = input.shift().split(`, `);
- let commandsCount = input.shift();
- for(let i = 0; i < commandsCount; i++){
- let tokens = input.shift().split(`, `);
- let action = tokens[0];
- let nameOrIndex = tokens[1];
- let secondName = tokens[2];
- if(action === `Add`){
- let index = tanks.indexOf(nameOrIndex);
- if(index > -1){
- console.log(`Tank is already bought`);
- }
- else{
- tanks.push(nameOrIndex);
- console.log(`Tank successfully bought`);
- }
- }
- else if(action === `Insert`){
- let index = +nameOrIndex;
- if(index > 0 && index > tanks.length){
- console.log(`Index out of range`);
- }
- else{
- let indexOfSecond = tanks.indexOf(secondName);
- if(index > -1){
- tanks.splice(index, 0, secondName)
- console.log(`Tank successfully bought`);
- }
- else{
- console.log(`Tank is already bought`);
- }
- }
- }
- else if(action === `Remove`){
- let index = tanks.indexOf(nameOrIndex);
- if(index > -1){
- tanks.splice(index, 1)
- console.log(`Tank successfully sold`);
- }
- else{
- console.log(`Tank not found`);
- }
- }
- else if(action === `Remove At`){
- let index = +nameOrIndex;
- if(index > 0 && index > tanks.length){
- console.log(`Index out of range`);
- }
- else{
- let index = +nameOrIndex;
- tanks.splice(index, 1)
- console.log(`Tank successfully sold`);
- }
- }
- }
- console.log(tanks.join(`, `))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement