Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrManipulator(arr,elements){
- for(let row of elements){
- row=row.split(" ")
- let command=row.shift()
- let firstNum=Number(row.shift())
- if(command!=='print'){
- switch(command){
- case "add":
- add(arr,firstNum,row)
- break;
- case "remove":
- remove(arr,firstNum)
- break;
- case "addMany":
- addMany(arr,firstNum,row)
- break;
- case "contains":
- contains(arr,firstNum)
- break;
- case "shift":
- shift(arr,firstNum)
- break;
- case "sumPairs":
- arr=sumPair(arr,)
- break;
- }
- }
- else{
- break;
- }
- }
- console.log(`[ ${arr.join(", ")} ]`)
- function add(arr,index,element){
- return arr.splice(index,0,Number(element))
- }
- function remove(arr,index){
- return arr.splice(index,1)
- }
- function addMany(arr,index,row){
- row=row.toString().split(",")
- for(let num of row.reverse()){
- arr.splice(index,0,Number(num))
- }
- }
- function contains(arr,searchedNum){
- if(arr.includes(searchedNum)){
- console.log(arr.indexOf(searchedNum))
- }
- else{
- console.log(-1)
- }
- }
- function shift(arr,count){
- for(let i=0;i<count;i++){
- arr.push(arr.shift())
- }
- }
- function sumPair(arr){
- let sum=[]
- if(arr.length%2!==0){
- arr.push(0)
- }
- while(arr.length>0){
- let firstPair=arr.shift()
- let secondPair=arr.shift()
- sum.push(firstPair+secondPair)
- }
- return sum
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement