vladovip

02. Array Modifier - JavaScript

May 4th, 2021
578
356 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(commands) {
  2.  
  3.     let currentArray = commands
  4.         .shift()
  5.         .split(' ')
  6.         .map(Number);
  7.     // console.log(currentArray);
  8.  
  9.     for (let i = 0; i < commands.length; i++) { // въртим по броя на командите
  10.         let commandsLineArray = [currentCommand, index1, index2] = commands[i].split(' '); // взимаме съответната команда, число и индекс. П.С. Първо бих взела само командата и само, когато тя е insert ще взимам secondNum(който реално е индекса)
  11.         firstNum = Number(index1);
  12.         secondNum = Number(index2);
  13.         //console.log(currentCommand, index1, index2);
  14.  
  15.         if (currentCommand === 'swap') {
  16.             swap();
  17.         }
  18.  
  19.         else if (currentCommand === 'multiply') {
  20.             multiply()
  21.         }
  22.  
  23.         else if (currentCommand === 'decrease') {
  24.             decrease();
  25.         }
  26.         else if (currentCommand === 'end') {
  27.             end();
  28.         }
  29.  
  30.  
  31.         function swap() {
  32.             // [arr[0], arr[1]] = [arr[1], arr[0]];
  33.             [currentArray[firstNum], currentArray[secondNum]] = [currentArray[secondNum], currentArray[firstNum]];
  34.             console.log(currentArray);
  35.         }
  36.  
  37.         function multiply(firstNum, secondNum) {
  38.             result = currentArray[firstNum] * currentArray[secondNum];
  39.             currentArray[1] = result;
  40.             console.log(currentArray);
  41.         }
  42.  
  43.         function decrease() {
  44.             for (let numbers of currentArray) {
  45.                 newNumbers = numbers - 1;
  46.                 newNumbers++;
  47.                 let newArray = [];
  48.                 newArray.push(newNumbers);
  49.                 console.log(newNumbers);
  50.             }
  51.         }
  52.  
  53.         function end() {
  54.             if (currentCommand === "end") {
  55.             }
  56.             break;
  57.         }
  58.  
  59.  
  60.     }
  61.  
  62. }
  63.  
  64.  
  65. solve(
  66.     [
  67.         '1 2 3 4',
  68.         'swap 0 1',       //2 1 3 4
  69.         'swap 1 2',       //2 3 1 4
  70.         'swap 2 3',       // 2 3 4 1
  71.         'multiply 1 2',   // 12 ; 2 12 4 1
  72.         'decrease',       //  1  11  3  0
  73.         'end'
  74.     ]
  75.  
  76. )
  77.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×