Liliana797979

array modifier - mid exam

Jul 18th, 2021
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayModifier(input) {
  2.     let array = input.shift().split(" ").map(Number);
  3.    // console.log(array);
  4.    let command = input.shift();
  5.    while (command !== "end") {
  6.        let tokens = command.split(" ");
  7.        switch (tokens[0]) {
  8.            case "swap":
  9.                swap(array, index1, index2)
  10.                break;
  11.             case "multiply":
  12.                 multiply(array, index1, index2);
  13.                 break;
  14.        
  15.            case "decrease":
  16.                decrease(array);
  17.                break;
  18.        }
  19.    
  20.    function swap(list, index1, index2) {
  21.        let index1 = Number(tokens[1]);
  22.        let index2 = Number(tokens[2]);
  23.         array[index1] = array[index2];
  24.         array[index2] = temp;
  25.         break;
  26. }
  27. function multiply(list, index1, index2) {
  28.     let index1 = Number(tokens[1]);
  29.     let index2 = Number(tokens[2]);
  30.     array[index1] *= array[index2];
  31.     break;
  32. }
  33. function decrease(list) {
  34. array = array.map(x => --x);    
  35. }
  36. command = input.shift();
  37.  
  38. }
  39. console.log(array.join(", "));
  40. }
  41.  
  42. arrayModifier([
  43.     '23 -2 321 87 42 90 -123',
  44.     'swap 1 3',
  45.     'swap 3 6',
  46.     'swap 1 0',
  47.     'multiply 1 2',
  48.     'multiply 2 1',
  49.     'decrease',
  50.     'end'
  51.   ]);
Advertisement
Add Comment
Please, Sign In to add comment