Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function player(array) {
- /**
- * Input
- • On the first input line you will receive Peter`s account - a sequence of game names, separated by space.
- • Until you receive "Play!" you will be receiving commands.
- */
- let account = array.shift();
- let games=account.split(" ")
- let curr;
- for (let index = 0; index < array.length; index++) {
- // •Install {game} - add the game at the last position in the account,
- // but only if it isn't installed already.
- curr = array[index].split(" ");
- let command = curr[0];
- let game = curr[1];
- if (command === 'Play!') {
- break;
- } else if (command === 'Install') {
- if (game !== games.includes(game)) {
- games.push(game);
- }
- }
- //•Uninstall {game} - delete the game if it exists.
- else if (command === 'Uninstall') {
- if (games.includes(game)) {
- let index = curr.indexOf(game)
- games.splice(index, 1)
- }
- //•Update {game} - update the game if it exists and place it in the last position.
- } else if (command === "Update") {
- if (games.includes(game)) {
- let index = curr.indexOf(game)
- games.splice(index, 1);
- games.push(game);
- }
- //•Expansion {game}-{expansion} - check if the game exists and insert after it
- //the expansion in the following format: "{game}:{expansion}";
- } else if (command === "Expansion") {
- let expanded = game.split('-');
- let game1 = expanded[0];
- let exp = expanded[1];
- if (games.includes(game1)) {
- let index = curr.indexOf(game);
- let index1 = curr.indexOf(game1);
- let index2 = curr.indexOf(exp);
- let expandedGame = `${game1}:${exp}`;
- games.splice(index, 0, expandedGame);
- }
- }
- }
- console.log(games.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement