Advertisement
cecko

Untitled

Feb 12th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. function array(input) {
  2.  
  3. let nums = input.shift().split(' ').map(Number);
  4.  
  5. while (input[0] != "end") {
  6. let tokens = input.shift().split(' ');
  7. let command = tokens[0];
  8. let index1 = Number(tokens[1]);
  9. let index2 = Number(tokens[2]);
  10. switch (command) {
  11. case 'swap':
  12. let temp = nums[index1];
  13. nums[index1] = nums[index2];
  14. nums[index2] = temp;
  15. break;
  16. case 'multiply':
  17. nums[index1] *= nums[index2];
  18. break;
  19. case 'decrease':
  20. for (let i = 0; i < nums.length; i++) {
  21. nums[i--];
  22. }
  23. break;
  24. }
  25. }
  26. console.log(nums.join(', '))
  27.  
  28. }
  29. array([
  30. ['23 -2 321 87 42 90 -123',
  31. 'swap 1 3',
  32. 'swap 3 6',
  33. 'swap 1 0',
  34. 'multiply 1 2',
  35. 'multiply 2 1',
  36. 'decrease',
  37. 'end']
  38. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement