Advertisement
ErolKZ

Untitled

Nov 26th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let num = input.shift();
  5.  
  6. num = Number(num);
  7.  
  8. let j = 0;
  9.  
  10. let arr = [];
  11.  
  12.  
  13. for (let i = 0; i < num; i++) {
  14.  
  15. let curEl = input.shift().split('|');
  16.  
  17. arr.push({ piece: curEl[0], composer: curEl[1], key: curEl[2] });
  18.  
  19. }
  20.  
  21. // console.log(arr);
  22.  
  23.  
  24. while (input[j] !== 'Stop') {
  25.  
  26. let curEl = input[j].split('|');
  27.  
  28. let command = curEl[0];
  29.  
  30.  
  31. if (command === 'Add') {
  32.  
  33. if (arr.some(el => el.piece === curEl[1])) {
  34.  
  35. console.log(`${curEl[1]} is already in the collection!`);
  36.  
  37. } else {
  38.  
  39. arr.push({ piece: curEl[1], composer: curEl[2], key: curEl[3] });
  40.  
  41. console.log(`${curEl[1]} by ${curEl[2]} in ${curEl[3]} added to the collection!`);
  42.  
  43. }
  44.  
  45. } else if (command === 'Remove') {
  46.  
  47. if (arr.some(el => el.piece === curEl[1])) {
  48.  
  49. let index = 0;
  50.  
  51. arr.forEach(el => el.piece === curEl[1] ? index = arr.indexOf(el) : false);
  52.  
  53. arr.splice(index, 1);
  54.  
  55. console.log(`Successfully removed ${curEl[1]}!`);
  56.  
  57. } else {
  58.  
  59. console.log(`Invalid operation! ${curEl[1]} does not exist in the collection.`);
  60.  
  61. }
  62.  
  63.  
  64. } else if (command === 'ChangeKey') {
  65.  
  66. if (arr.some(el => el.piece === curEl[1])) {
  67.  
  68. arr.forEach(el => el.piece === curEl[1] ? el.key = curEl[2] : false);
  69.  
  70. console.log(`Changed the key of ${curEl[1]} to ${curEl[2]}!`);
  71.  
  72. } else {
  73.  
  74. console.log(`Invalid operation! ${curEl[1]} does not exist in the collection.`);
  75.  
  76. }
  77.  
  78. }
  79.  
  80. j++;
  81. }
  82.  
  83.  
  84. arr.sort((a, b) => a.piece.localeCompare(b.piece));
  85.  
  86.  
  87. for (let el of arr) {
  88.  
  89. console.log(`${el.piece} -> Composer: ${el.composer}, Key: ${el.key}`);
  90.  
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement