Advertisement
teofarov13

Untitled

Feb 7th, 2023
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function player(array) {
  2.     /**
  3.      * Input
  4. • On the first input line you will receive Peter`s account - a sequence of game names, separated by space.
  5. • Until you receive "Play!" you will be receiving commands.
  6.      */
  7.     let account = array.shift();
  8.     let games=account.split(" ")
  9.     let curr;
  10.     for (let index = 0; index < array.length; index++) {
  11.         // •Install {game} - add the game at the last position in the account,
  12.         // but only if it isn't installed already.
  13.         curr = array[index].split(" ");
  14.         let command = curr[0];
  15.         let game = curr[1];
  16.         if (command === 'Play!') {
  17.             break;
  18.         } else if (command === 'Install') {
  19.             if (game !== games.includes(game)) {
  20.                 games.push(game);
  21.             }
  22.            
  23.         }
  24.         //•Uninstall {game} - delete the game if it exists.
  25.         else if (command === 'Uninstall') {
  26.             if (games.includes(game)) {
  27.                 let index = curr.indexOf(game)
  28.                 games.splice(index, 1)
  29.             }
  30.             //•Update {game} - update the game if it exists and place it in the last position.
  31.         } else if (command === "Update") {
  32.             if (games.includes(game)) {
  33.                 let index = curr.indexOf(game)
  34.                 games.splice(index, 1);
  35.                 games.push(game);
  36.             }
  37.             //•Expansion {game}-{expansion} - check if the game exists and insert after it
  38.             //the expansion in the following format: "{game}:{expansion}";
  39.         } else if (command === "Expansion") {
  40.             let expanded = game.split('-');
  41.             let game1 = expanded[0];
  42.             let exp = expanded[1];
  43.             if (games.includes(game1)) {
  44.                 let index = curr.indexOf(game);
  45.                 let index1 = curr.indexOf(game1);
  46.                 let index2 = curr.indexOf(exp);
  47.                 let expandedGame = `${game1}:${exp}`;
  48.                 games.splice(index, 0, expandedGame);
  49.                
  50.             }
  51.         }
  52.  
  53.     }
  54.    console.log(games.join(" "));
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement