Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayManipulator(array, manipulations) {
- for (let i = 0; i < manipulations.length; i++) {
- let manipulation = manipulations[i].split(" ");
- let command = manipulation.shift();
- let firstNum = Number(manipulation.shift());
- let secNum = Number(manipulation[0]);
- switch (command) {
- case "add":
- array.splice(firstNum, 0, secNum);
- break;
- case "addMany":
- let index = firstNum;
- for (let j = 0; j < manipulation.length; j++) {
- array.splice(index + j, 0, Number(manipulation[j]));
- }
- break;
- case "contains":
- if (array.includes(firstNum)) {
- console.log(array.indexOf(firstNum));
- } else {
- console.log(-1);
- }
- break;
- case "remove":
- array.splice(firstNum, 1);
- break;
- case "shift":
- let positions = firstNum;
- let shifted = array.splice(0, positions);
- for (let j = 0; j < shifted.length; j++) {
- array.push(shifted[j]);
- }
- break;
- case "sumPairs":
- let sumArr = [];
- for (let j = 0; j < array.length; j += 2) {
- if (j + 1 > array.length - 1) {
- sumArr.push(array[j] + 0);
- } else {
- sumArr.push(array[j] + array[j + 1]);
- }
- }
- let sliced = sumArr.slice(0);
- array = sliced;
- break;
- case "print":
- console.log(`[ ${array.join(", ")} ]`);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement