Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Vlado Genoff on 2.7.2016 г..
- */
- function addRemoveElements(command) {
- let arr = [];
- for (let i=0; i<command.length; i++) {
- let temp = command[i].split(' ');
- let cmd = temp[0];
- let arg = temp[1];
- switch (cmd) {
- case 'add': arr.push(arg); break;
- case 'remove':
- let index = arg;
- if (index == 0) {
- arr.shift();
- //console.log('Line ${i} shift - 0')
- } else {
- if (index == arr.length - 1) {
- arr.pop();
- //console.log('Line ${i} pop - LAST')
- } else {
- arr[index] = undefined;
- }
- }
- break;
- }
- }
- for (let i=0; i<arr.length; i++)
- {
- if (arr[i] != undefined) {
- console.log(arr[i]);
- }
- }
- }
- //addRemoveElements(['add 3','add 5','remove 2','remove 0','add 7'])
- addRemoveElements(['add 3','add 5','remove 1','add 2'])
Advertisement
Add Comment
Please, Sign In to add comment