Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr1,arr2){
  2.   let mainArr = arr1;
  3.   let commands = arr2;
  4.   for(let i = 0; i < commands.length;i++){
  5.       let currentCommand = commands[i].split(" ").shift();
  6.       if(currentCommand === "add"){
  7.           let index = +commands[i].split(" ")[1];
  8.           let element = +commands[i].split(" ")[2];
  9.           mainArr.splice(index,0,element);
  10.       }else if(currentCommand === "addMany"){
  11.           let index = +commands[i].split(" ")[1];
  12.           let final = commands[i].split(" ");
  13.           for(let n = 2; n< final.length; n++){
  14.               let currentNum = +final[n];
  15.               mainArr.splice(index,0,currentNum);
  16.               index++;
  17.           }
  18.       }else if(currentCommand === "contains"){
  19.           let numToFind = +commands[i].split(" ")[1];
  20.           let index = mainArr.indexOf(numToFind);
  21.           console.log(index);
  22.       }else if(currentCommand === "remove"){
  23.          let index = +commands[i].split(" ")[1];
  24.          mainArr.splice(index,1);
  25.       }else if(currentCommand === "shift"){
  26.           let position = +commands[i].split(" ")[1];
  27.           for(let i = 1; i<= position;i++){
  28.               let firstNum = mainArr.shift();
  29.               mainArr.push(firstNum);
  30.           }
  31.       }else if(currentCommand === "sumPairs"){
  32.           let currentArr = []
  33.           for(let i = 0; i < mainArr.length-1;i++){
  34.              let sum = 0;
  35.              sum = +mainArr[i] + +mainArr[i+1];
  36.               currentArr.push(sum)
  37.               i++;
  38.           }
  39.           if(mainArr.length % 2 !== 0){
  40.             currentArr.push(mainArr[mainArr.length-1])
  41.  
  42.           }
  43.           mainArr = currentArr;
  44.       }
  45.   }
  46.    console.log(mainArr);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement