Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let num = input.shift();
- num = Number(num);
- let j = 0;
- let arr = [];
- for (let i = 0; i < num; i++) {
- let curEl = input.shift().split('|');
- arr.push({ piece: curEl[0], composer: curEl[1], key: curEl[2] });
- }
- // console.log(arr);
- while (input[j] !== 'Stop') {
- let curEl = input[j].split('|');
- let command = curEl[0];
- if (command === 'Add') {
- if (arr.some(el => el.piece === curEl[1])) {
- console.log(`${curEl[1]} is already in the collection!`);
- } else {
- arr.push({ piece: curEl[1], composer: curEl[2], key: curEl[3] });
- console.log(`${curEl[1]} by ${curEl[2]} in ${curEl[3]} added to the collection!`);
- }
- } else if (command === 'Remove') {
- if (arr.some(el => el.piece === curEl[1])) {
- let index = 0;
- arr.forEach(el => el.piece === curEl[1] ? index = arr.indexOf(el) : false);
- arr.splice(index, 1);
- console.log(`Successfully removed ${curEl[1]}!`);
- } else {
- console.log(`Invalid operation! ${curEl[1]} does not exist in the collection.`);
- }
- } else if (command === 'ChangeKey') {
- if (arr.some(el => el.piece === curEl[1])) {
- arr.forEach(el => el.piece === curEl[1] ? el.key = curEl[2] : false);
- console.log(`Changed the key of ${curEl[1]} to ${curEl[2]}!`);
- } else {
- console.log(`Invalid operation! ${curEl[1]} does not exist in the collection.`);
- }
- }
- j++;
- }
- arr.sort((a, b) => a.piece.localeCompare(b.piece));
- for (let el of arr) {
- console.log(`${el.piece} -> Composer: ${el.composer}, Key: ${el.key}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement