ALSAVOV

Untitled

Jun 20th, 2023
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.00 KB | Source Code | 0 0
  1. function arrayModify(data) {
  2.     let arr = data.shift().split(" ").map(Number);
  3.  
  4.     let index = 0;
  5.  
  6.     let command = data[index];
  7.     index++;
  8.  
  9.     while (command !== "end") {
  10.         let tokens = command.split(" ");
  11.         let action = tokens[0];
  12.  
  13.         let indexOfFirstEl = Number(tokens[1]);
  14.         let indexOfSecondEl = Number(tokens[2]);
  15.  
  16.         switch (action) {
  17.             case "swap":
  18.                 let elementToReplace = arr[indexOfFirstEl];
  19.                 let replacedElement = arr[indexOfSecondEl];
  20.                 arr.splice(indexOfFirstEl, 1, replacedElement);
  21.                 arr.splice(indexOfSecondEl, 1, elementToReplace);
  22.                 break;
  23.             case "multiply":
  24.                 arr[indexOfFirstEl] *= arr[indexOfSecondEl];
  25.                 break;
  26.             case "decrease":
  27.                 arr = arr.map((element) => element - 1);
  28.                 break;
  29.         }
  30.  
  31.         command = data[index];
  32.         index++;
  33.     }
  34.     console.log(arr.join(", "));
  35. }
Advertisement
Add Comment
Please, Sign In to add comment