Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) { //100т. - перфектна
- let plantsArr = [];
- let n = Number(inputArr.shift());
- for (let i = 0; i < inputArr.length; i++) {
- 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}"
- let tokens = inputArr[i].split("<->");
- let name = tokens[0];
- let rarity = Number(tokens[1]);
- let plant = plantsArr.find(plant => plant.name === name);
- if (plant) { //ако има такова цвете в масива - ъпдейтваме негоного rarity
- plant.rarity = rarity;
- } else { //ако няма такова цвете - създаваме го
- let plantToAdd = {
- name: name,
- rarity: rarity,
- ratings: [],
- avgRating: 0,
- }
- plantsArr.push(plantToAdd);
- }
- } else { //After that until you receive the command "Exhibition" you will be given some of these commands:
- if (inputArr[i] == "Exhibition") {
- for (const plant of plantsArr) { //определяме средния рейтинг на всяко растение
- if (plant.ratings.length < 1) { //аго масива му с рейтинги е празен - средния рейтинг ще му е нула
- plant.avgRating = 0;
- } else {
- plant.avgRating = plant.ratings.reduce((a, b) => a + b, 0) / plant.ratings.length; //събираме всичките му рейтинги и ги делим на дължината на масива с рейтинги на цветето
- }
- }
- plantsArr.sort((a, b) => b.rarity - a.rarity || b.avgRating - a.avgRating); //сортиране по низходящ ред първо по rarity, после по среден рейтинг
- console.log("Plants for the exhibition:");
- for (const plant of plantsArr) {
- console.log(`- ${plant.name}; Rarity: ${plant.rarity}; Rating: ${plant.avgRating.toFixed(2)}`)
- }
- } else {
- let tokens = inputArr[i].split(": ");
- let command = tokens[0];
- let nameAndParam = tokens[1].split(" - ");
- let name = nameAndParam[0];
- let plant = plantsArr.find(plant => plant.name === name);
- if (plant) { //ако има в масива с цветя цвете с такова име
- if (command == "Rate") {
- let rating = Number(nameAndParam[1]);
- plant.ratings.push(rating);
- } else if (command == "Update") {
- let newRarity = Number(nameAndParam[1]);
- plant.rarity = newRarity;
- } else if (command == "Reset") {
- plant.ratings = [];
- } else { //значи командата не съществува
- console.log("error");
- }
- } else { //значи няма такова цвете
- console.log("error");
- }
- }
- }
- }
- }
- 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"]);
- console.log();
- solve(["2", "Candelabra<->10", "Oahu<->10", "Rate: Oahu - 7", "Rate: Candelabra - 6", "Exhibition"]);
Add Comment
Please, Sign In to add comment