Advertisement
TZinovieva

Plant Discovery JS Fundamentals

Apr 1st, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(array) {
  2.   let n = +array.shift();
  3.  
  4.   let list = {};
  5.   for (let i = 0; i < n; i++) {
  6.  
  7.     let [plant, rarity] = array.shift().split(`<->`);
  8.     rarity = +rarity;
  9.  
  10.     if (list.hasOwnProperty(plant) == false) {
  11.       list[plant] = { rarity: 0, rating: [] };
  12.     }
  13.     list[plant].rarity += rarity;
  14.   }
  15.  
  16.   while (array[0] != "Exhibition") {
  17.  
  18.     let line = array.shift();
  19.     let tokens = line.split(`: `);
  20.     let currPlant = tokens[1].split(` - `);
  21.     let name = currPlant[0];
  22.  
  23.     if (list.hasOwnProperty(name)) {
  24.  
  25.       if (tokens[0] == "Rate") {
  26.         let rating = +currPlant[1];
  27.         list[name].rating.push(rating);
  28.       }
  29.  
  30.       if (tokens[0] == "Update") {
  31.         let newRarity = +currPlant[1];
  32.         list[name].rarity = newRarity;
  33.       }
  34.  
  35.       if (tokens[0] == "Reset") {
  36.         list[name].rating = [];
  37.       }
  38.     } else {
  39.       console.log(`error`);
  40.     }
  41.   }
  42.  
  43.   console.log(`Plants for the exhibition:`);
  44.   for (let currPlant in list) {
  45.  
  46.     let average = 0;
  47.     for (let currRating of list[currPlant].rating) {
  48.       average += currRating;
  49.     }
  50.  
  51.     if (list[currPlant].rating.length !== 0) {
  52.       average /= list[currPlant].rating.length;
  53.     }
  54.  
  55.     console.log(`- ${currPlant}; Rarity: ${list[currPlant].rarity}; Rating: ${average.toFixed(2)}`);
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement