victoriaSD

Untitled

Mar 16th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(input) {
  2.   let countPlants = Number(input.shift());
  3.  
  4.   let plants = {};
  5.   let originalRarity = {};
  6.   let rating = {};
  7.  
  8.   for (let i = 0; i < countPlants; i++) {
  9.  
  10.     let rowOfPlantInfo = input.shift();
  11.     let [plant, rarity] = rowOfPlantInfo.split("<->");
  12.  
  13.     plants[plant] = Number(rarity);
  14.     originalRarity[plant] = Number(rarity);
  15.     rating[plant] = 0;
  16.  
  17.   }
  18.  
  19.   for (let row of input) {
  20.     let [command, elements] = row.split(": ");
  21.  
  22.     if (command === "Exhibition") {
  23.  
  24.       console.log(`Plants for the exhibition:`);
  25.       break;
  26.     }
  27.  
  28.     if (command === "Rate") {
  29.  
  30.       let [plant, newRating] = elements.split(" - ");
  31.  
  32.       if (plants.hasOwnProperty(plant)) {
  33.  
  34.         rating[plant] += Number(newRating);
  35.       }
  36.       else {
  37.         console.log("error");
  38.       }
  39.     }
  40.     else if (command === "Update") {
  41.  
  42.       let [plant, newRarity] = elements.split(" - ");
  43.  
  44.       if (plants.hasOwnProperty(plant)) {
  45.  
  46.         plants[plant] = Number(newRarity);
  47.       }
  48.       else {
  49.         console.log("error");
  50.       }
  51.     }
  52.     else if (command === "Reset") {
  53.      
  54.         if (plants.hasOwnProperty(elements)) {
  55.         plants[elements] = originalRarity[elements];
  56.       }
  57.       else {
  58.         console.log("error");
  59.       }
  60.     }
  61.   }
  62.  
  63.   let array = Object.entries(plants);
  64.   let ratingArr = Object.entries(rating);
  65.  
  66.   for (let [plant, rarity] of array) {
  67.     avg = 0;
  68.  
  69.     for (let [plant1, rating] of ratingArr) {
  70.  
  71.       if (plant === plant1) {
  72.  
  73.         avg = rating.toFixed(2);
  74.         break;
  75.       }
  76.     }
  77.    
  78.     console.log(`- ${plant}; Rarity: ${rarity} Rating: ${avg}`);
  79.   }
  80. }
  81.  
  82. plantDiscovery([
  83.   "3",
  84.   "Arnoldii<->4",
  85.   "Woodii<->7",
  86.   "Welwitschia<->2",
  87.   "Rate: Woodii - 10",
  88.   "Rate: Welwitschia - 7",
  89.   "Rate: Arnoldii - 3",
  90.   "Rate: Woodii - 5",
  91.   "Update: Woodii - 5",
  92.   "Reset: Arnoldii",
  93.   "Exhibition",
  94. ]);
Tags: zadacha3
Advertisement
Add Comment
Please, Sign In to add comment