Advertisement
Guest User

Untitled

a guest
Sep 14th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printInfo(input) {
  2.     let plantCount = Number(input.shift());
  3.     let plantCollection = {};
  4.  
  5.     for (let i = 0; i < plantCount; i++) {
  6.         let line = input.shift();
  7.         let [plant, rarity] = line.split("<->");
  8.         rarity = Number(rarity);
  9.         plantCollection[plant] = {
  10.             rarity,
  11.         }
  12.     }
  13.  
  14.     let line = input.shift();
  15.     while (line !== "Exhibition") {
  16.         let [command, ...rest] = line.split(": ");
  17.         let [plant, ...data] = rest[0].split(" - ");
  18.         let fullRegEx = /[A-Z][a-z]+: [A-Z][a-z]+ - [\d]+/;
  19.         let partRegEx = /[A-Z][a-z]+: [A-Z][a-z]+/;
  20.  
  21.         if (plantCollection[plant] && line.match(fullRegEx)) {
  22.             switch (command) {
  23.                 case "Rate":
  24.                     let rating = Number(data[0]);
  25.                     if (!plantCollection[plant].rating) {
  26.                         plantCollection[plant].rating = [];
  27.                     }
  28.                     plantCollection[plant].rating.push(rating);
  29.                     break;
  30.                 case "Update":
  31.                     let newRarity = Number(data[0]);
  32.                     plantCollection[plant].rarity = newRarity;
  33.                     break;
  34.                 case "Reset":
  35.                     plantCollection[plant].rating = [0];
  36.                     break;
  37.                 default:
  38.                     console.log("error");
  39.                     break;
  40.             }
  41.         } else if (plantCollection[plant] && line.match(partRegEx)) {
  42.             switch (command) {
  43.                 case "Reset":
  44.                     plantCollection[plant].rating = [0];
  45.                     break;
  46.                 default:
  47.                     console.log("error");
  48.                     break;
  49.             }
  50.         } else {
  51.             console.log("error");
  52.         }
  53.         line = input.shift();
  54.     }
  55.  
  56.     for (let key in plantCollection) {
  57.         if (plantCollection[key].rating) {
  58.             let ratingsNr = plantCollection[key].rating.length;
  59.             plantCollection[key].rating = plantCollection[key].rating.reduce((a, b) => a + b);
  60.             plantCollection[key].rating /= ratingsNr;
  61.         }
  62.     }
  63.  
  64.     let sortedPlants = Object.keys(plantCollection)
  65.         .sort((a, b) => plantCollection[b].rarity - plantCollection[a].rarity || plantCollection[b].rating - plantCollection[a].rating);
  66.  
  67.     console.log(`Plants for the exhibition:`);
  68.  
  69.     for (let plant of sortedPlants) {
  70.         let data = Object.entries(plantCollection[plant]);
  71.         console.log(`- ${plant}; Rarity: ${data[0][1]}; Rating: ${data[1][1].toFixed(2)}`);
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement