Advertisement
Spocoman

02. Array Modifier

Nov 7th, 2023
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayModifier(input) {
  2.     let numbers = input[0].split(' ').map(Number);
  3.  
  4.     for (let i = 1; input[i] != "end"; i++) {
  5.         [command, index1, index2] = input[i].split(' ');
  6.  
  7.         if (command == "decrease") {
  8.             for (let i = 0; i < numbers.length; i++) {
  9.                 numbers[i]--;
  10.             }
  11.         } else {
  12.             if (command == "swap") {
  13.                 let value = numbers[index1];
  14.                 numbers[index1] = numbers[index2];
  15.                 numbers[index2] = value;
  16.             } else {
  17.                 numbers[index1] *= numbers[index2];
  18.             }
  19.         }
  20.     }
  21.    
  22.     console.log(numbers.join(", "));
  23.     return;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement