Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function plantDiscovery(input) {
- let countPlants = Number(input.shift());
- let plants = {};
- let originalRarity = {};
- let rating = {};
- for (let i = 0; i < countPlants; i++) {
- let rowOfPlantInfo = input.shift();
- let [plant, rarity] = rowOfPlantInfo.split("<->");
- plants[plant] = Number(rarity);
- originalRarity[plant] = Number(rarity);
- rating[plant] = 0;
- }
- for (let row of input) {
- let [command, elements] = row.split(": ");
- if (command === "Exhibition") {
- console.log(`Plants for the exhibition:`);
- break;
- }
- if (command === "Rate") {
- let [plant, newRating] = elements.split(" - ");
- if (plants.hasOwnProperty(plant)) {
- rating[plant] += Number(newRating);
- }
- else {
- console.log("error");
- }
- }
- else if (command === "Update") {
- let [plant, newRarity] = elements.split(" - ");
- if (plants.hasOwnProperty(plant)) {
- plants[plant] = Number(newRarity);
- }
- else {
- console.log("error");
- }
- }
- else if (command === "Reset") {
- if (plants.hasOwnProperty(elements)) {
- plants[elements] = originalRarity[elements];
- }
- else {
- console.log("error");
- }
- }
- }
- let array = Object.entries(plants);
- let ratingArr = Object.entries(rating);
- for (let [plant, rarity] of array) {
- avg = 0;
- for (let [plant1, rating] of ratingArr) {
- if (plant === plant1) {
- avg = rating.toFixed(2);
- break;
- }
- }
- console.log(`- ${plant}; Rarity: ${rarity} Rating: ${avg}`);
- }
- }
- plantDiscovery([
- "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",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment