Advertisement
Guest User

Untitled

a guest
Sep 14th, 2020
315
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.         if (plantCollection[plant]) {
  19.             switch (command) {
  20.                 case "Rate": {
  21.                     let rating = Number(data[0]);
  22.                     if (!plantCollection[plant].rating) {
  23.                         plantCollection[plant].rating = [];
  24.                     }
  25.                     plantCollection[plant].rating.push(rating);
  26.                     break;
  27.                 }
  28.                 case "Update": {
  29.                     let newRarity = Number(data[0]);
  30.                     plantCollection[plant].rarity = newRarity;
  31.                     break;
  32.                 }
  33.                 case "Reset": {
  34.                     plantCollection[plant].rating = [0];
  35.                     break;
  36.                 }
  37.             }
  38.         } else {
  39.             console.log("error");
  40.         }
  41.         line = input.shift();
  42.     }
  43.  
  44.     for (let key in plantCollection) {
  45.         if (plantCollection[key].rating){
  46.             let ratingsNr = plantCollection[key].rating.length;
  47.             plantCollection[key].rating = plantCollection[key].rating.reduce((a, b) => a + b);
  48.             plantCollection[key].rating /= ratingsNr;
  49.         }
  50.     }
  51.     let sortedPlants = Object.keys(plantCollection)
  52.         .sort((a, b) => plantCollection[b].rarity - plantCollection[a].rarity || plantCollection[b].rating - plantCollection[a].rating);
  53.  
  54.     console.log(`Plants for the exhibition:`);
  55.  
  56.     for (let element of sortedPlants) {
  57.         if(plantCollection[element] !== undefined && plantCollection[element].rarity !== undefined && plantCollection[element].rating !== undefined){
  58.             console.log(`- ${element}; Rarity: ${plantCollection[element].rarity}; Rating: ${plantCollection[element].rating.toFixed(2)}`);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement