Ivankooo1

arrayTask

Nov 3rd, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(arr){
  2.     let mainArr = arr.shift().split(' ');
  3.     let result = '';
  4.  for(let elements of arr){
  5.      if(elements === 'End'){
  6.          break;
  7.      }
  8.      let token = elements.split(' ');
  9.      let command = token.shift();
  10.    
  11.      switch(command){
  12.          case 'Switch':
  13.             let ind = Number(token[0]);
  14.             if(ind < 0 || ind >= mainArr.length){
  15.                 break;
  16.             }
  17.             if(mainArr.indexOf(ind) === -1){
  18.                   if(mainArr[ind] > 0){
  19.                     //  mainArr[ind] *= -1;
  20.                     let num = -Math.abs(mainArr[ind]);
  21.                     mainArr.splice(ind,1,num);
  22.                   }else{
  23.                   //  mainArr[ind] *= 1;
  24.                     let num = Math.abs(mainArr[ind]);
  25.                     mainArr.splice(ind,1,num);
  26.                   }
  27.              
  28.             }
  29.  
  30.          ;break;
  31.          case 'Change':
  32.           let index = Number(token[0]);
  33.          
  34.           if(index<0 || index >= mainArr.length){
  35.               break;
  36.           }else{
  37.             if(mainArr.indexOf(index) === -1){
  38.                 let newIndex = Number(token[1]);
  39.                   mainArr.splice(index,1,newIndex);
  40.                  
  41.             }
  42.           }
  43.        
  44.          ;break;
  45.          case 'Sum':
  46.              switch(token[0]){
  47.                  case 'Negative':
  48.                  let sumNegative = 0
  49.                  let negative = mainArr.filter(a => a < 0);
  50.  
  51.                  for(let ind of negative){
  52.                      sumNegative += Number(ind);
  53.                  }
  54.                  console.log(sumNegative);
  55.  
  56.                  ;break;
  57.                  case 'Positive':
  58.                  let sumPositive = 0;
  59.                  let positive = mainArr.filter(a => a > 0);
  60.                  for(let ind of positive){
  61.                      sumPositive += Number(ind);
  62.                  }
  63.                  console.log(sumPositive);
  64.                  ;break;
  65.                  case 'All':
  66.                  let sumAll = 0;
  67.                  for(let ind of mainArr){
  68.                      sumAll += Number(ind);
  69.                  }    
  70.                  console.log(sumAll);
  71.                  ;break;
  72.              };break;
  73.      }
  74.  
  75.  }
  76.  
  77.  for(let i = 0;i < mainArr.length;i++){
  78.      if(mainArr[i] >= 0){
  79.        result += mainArr[i] + ' ';
  80.      }
  81.  }
  82.   console.log(result)
  83. }
  84. solution([
  85.     //  '1 2 3 4 5',
  86.     //  'Switch 4',
  87.     //  'Change 0 -3',
  88.     //  'Sum Negative',
  89.     //  'End'
  90.  
  91.     '1 2 3 4 5 4 3 2 1 0',
  92.     'Switch -4',
  93.     'Change 13 0',
  94.     'Switch 0',
  95.     'Sum All',
  96.     'End'
  97.    ])
Advertisement
Add Comment
Please, Sign In to add comment