Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function manipulator(arr, input) {
- let arrToPrint = arr.slice();
- for (let i = 0; i < input.length; i++) {
- let command = input[i].split(' ');
- let action = command[0];
- if (action === 'add') {
- let ind = Number(command[1]);
- let el = Number(command[2]);
- addCommand(ind, el);
- }
- else if (action === 'addMany') {
- let ind = Number(command[1]);
- for (let i = command.length - 1; i >= 2; i--) {
- let element = Number(command[i]);
- arrToPrint.splice(ind,0,element);
- }
- }
- else if (action === 'contains') {
- let el = Number(command[1]);
- isContain(el);
- }
- else if (action === 'remove') {
- let ind = Number(command[1]);
- arrToPrint.splice(ind, 1);
- }
- else if (action === 'shift') {
- let positions = Number(command[1]);
- while (positions > 0) {
- arrToPrint.push(arrToPrint.shift());
- positions--;
- }
- }
- else if (action === 'sumPairs') {
- let testArr = arrToPrint.slice();
- arrToPrint = [];
- while (testArr.length > 1) {
- let num = testArr.shift() + testArr.shift();
- arrToPrint.push(num);
- }
- if (testArr.length === 1) {
- arrToPrint.push(testArr.shift());
- }
- }
- else if (action === 'print') {
- console.log(`[ ${arrToPrint.join(', ')} ]`);
- break;
- }
- }
- function isContain(num) {
- if (arrToPrint.indexOf(num) !== -1) {
- console.log(arrToPrint.indexOf(num));
- }
- else {
- console.log(-1);
- }
- }
- function addCommand(index, element) {
- arrToPrint.splice(index, 0, element);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement