dddilian

03. Plant Discovery - FINAL EXAM масив, пълен с обекти

Aug 9th, 2020 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr) { //100т. - перфектна
  2.     let plantsArr = [];
  3.     let n = Number(inputArr.shift());
  4.  
  5.     for (let i = 0; i < inputArr.length; i++) {
  6.  
  7.         if (i < n) { //On the next n lines, you will be given some information about the plants that you have discovered in the format: "{plant}<->{rarity}"
  8.             let tokens = inputArr[i].split("<->");
  9.             let name = tokens[0];
  10.             let rarity = Number(tokens[1]);
  11.  
  12.             let plant = plantsArr.find(plant => plant.name === name);
  13.  
  14.             if (plant) { //ако има такова цвете в масива - ъпдейтваме негоного rarity
  15.                 plant.rarity = rarity;
  16.             } else { //ако няма такова цвете - създаваме го
  17.                 let plantToAdd = {
  18.                     name: name,
  19.                     rarity: rarity,
  20.                     ratings: [],
  21.                     avgRating: 0,
  22.                 }
  23.                 plantsArr.push(plantToAdd);
  24.             }
  25.  
  26.         } else { //After that until you receive the command "Exhibition" you will be given some of these commands:
  27.  
  28.             if (inputArr[i] == "Exhibition") {
  29.  
  30.                 for (const plant of plantsArr) { //определяме средния рейтинг на всяко растение
  31.                     if (plant.ratings.length < 1) { //аго масива му с рейтинги е празен - средния рейтинг ще му е нула
  32.                         plant.avgRating = 0;
  33.                     } else {
  34.                         plant.avgRating = plant.ratings.reduce((a, b) => a + b, 0) / plant.ratings.length; //събираме всичките му рейтинги и ги делим на дължината на масива с рейтинги на цветето
  35.                     }
  36.                 }
  37.  
  38.                 plantsArr.sort((a, b) => b.rarity - a.rarity || b.avgRating - a.avgRating); //сортиране по низходящ ред първо по rarity, после по среден рейтинг
  39.  
  40.                 console.log("Plants for the exhibition:");
  41.                 for (const plant of plantsArr) {
  42.                     console.log(`- ${plant.name}; Rarity: ${plant.rarity}; Rating: ${plant.avgRating.toFixed(2)}`)
  43.                 }
  44.  
  45.             } else {
  46.                 let tokens = inputArr[i].split(": ");
  47.                 let command = tokens[0];
  48.                 let nameAndParam = tokens[1].split(" - ");
  49.                 let name = nameAndParam[0];
  50.  
  51.                 let plant = plantsArr.find(plant => plant.name === name);
  52.  
  53.                 if (plant) { //ако има в масива с цветя цвете с такова име
  54.                     if (command == "Rate") {
  55.                         let rating = Number(nameAndParam[1]);
  56.                         plant.ratings.push(rating);
  57.                     } else if (command == "Update") {
  58.                         let newRarity = Number(nameAndParam[1]);
  59.                         plant.rarity = newRarity;
  60.                     } else if (command == "Reset") {
  61.                         plant.ratings = [];
  62.                     } else { //значи командата не съществува
  63.                         console.log("error");
  64.                     }
  65.                 } else { //значи няма такова цвете
  66.                     console.log("error");
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. solve(["3", "Arnoldii<->4", "Woodii<->7", "Welwitschia<->2", "Rate: Woodii - 10", "Rate: Welwitschia - 7", "Rate: Arnoldii - 3", "Rate: Woodii - 5", "Update: Woodii - 5", "Reset: Arnoldii", "Exhibition"]);
  74. console.log();
  75. solve(["2", "Candelabra<->10", "Oahu<->10", "Rate: Oahu - 7", "Rate: Candelabra - 6", "Exhibition"]);
Add Comment
Please, Sign In to add comment