victoriaSD

Plant Discovery

Mar 29th, 2024
91
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 ratingCount = {};
  6.   let rating = {};
  7.  
  8.   for (let i = 0; i < countPlants; i++) {
  9.     let rowOfPlantInfo = input.shift();
  10.     let [plant, rarity] = rowOfPlantInfo.split("<->");
  11.  
  12.     plants[plant] = Number(rarity);
  13.     ratingCount[plant] = 0;
  14.     rating[plant] = 0;
  15.   }
  16.  
  17.   for (let row of input) {
  18.     let [command, elements] = row.split(": ");
  19.  
  20.     if (command === "Exhibition") {
  21.       console.log(`Plants for the exhibition:`);
  22.       break;
  23.     }
  24.  
  25.     if (command === "Rate") {
  26.       let [plant, newRating] = elements.split(" - ");
  27.  
  28.       if (plants.hasOwnProperty(plant)) {
  29.         rating[plant] += Number(newRating);
  30.         ratingCount[plant] += 1;
  31.       } else {
  32.         console.log("error");
  33.       }
  34.     } else if (command === "Update") {
  35.       let [plant, newRarity] = elements.split(" - ");
  36.  
  37.       if (plants.hasOwnProperty(plant)) {
  38.         plants[plant] = Number(newRarity);
  39.       } else {
  40.         console.log("error");
  41.       }
  42.     } else if (command === "Reset") {
  43.       if (ratingCount.hasOwnProperty(elements)) {
  44.         rating[elements] = 0;
  45.         ratingCount[elements] = 0;
  46.       } else {
  47.         console.log("error");
  48.       }
  49.     }
  50.   }
  51.  
  52.   for (let plant in plants) {
  53.     let avg = rating[plant] / ratingCount[plant];
  54.  
  55.     if (isNaN(avg)) {
  56.       avg = 0;
  57.     }
  58.  
  59.     console.log(
  60.       `- ${plant}; Rarity: ${plants[plant]}; Rating: ${avg.toFixed(2)}`
  61.     );
  62.   }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment