Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function coinCollection(input) {
- const n = parseInt(input[0]);
- let coinList = input.slice(1, n + 1);
- let commands = input.slice(n + 1);
- for (let command of commands) {
- if (command.startsWith("Examine")) {
- if (coinList.length > 0) {
- const examinedCoin = coinList.shift();
- console.log(`Examined: ${examinedCoin}!`);
- }
- } else if (command.startsWith("Acquire")) {
- const coinName = command.substring(8); // Removes "Acquire " from the beginning
- coinList.push(coinName);
- } else if (command.startsWith("Swap")) {
- const [_, startIndex, endIndex] = command.split(" ").map(Number);
- if (!isNaN(startIndex) && !isNaN(endIndex) && startIndex >= 0 && endIndex >= 0 && startIndex < coinList.length && endIndex < coinList.length) {
- [coinList[startIndex], coinList[endIndex]] = [coinList[endIndex], coinList[startIndex]];
- console.log("Swapped!");
- }
- } else if (command === "Conclude") {
- break;
- }
- }
- if (coinList.length > 0) {
- console.log("Remaining coins: " + coinList.join(", "));
- } else {
- console.log("The collection is empty");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement