Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function plantDiscovery(input) {
- let countPlants = Number(input.shift());
- let plants = {};
- let ratingCount = {};
- let rating = {};
- for (let i = 0; i < countPlants; i++) {
- let rowOfPlantInfo = input.shift();
- let [plant, rarity] = rowOfPlantInfo.split("<->");
- plants[plant] = Number(rarity);
- ratingCount[plant] = 0;
- 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);
- ratingCount[plant] += 1;
- } 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 (ratingCount.hasOwnProperty(elements)) {
- rating[elements] = 0;
- ratingCount[elements] = 0;
- } else {
- console.log("error");
- }
- }
- }
- for (let plant in plants) {
- let avg = rating[plant] / ratingCount[plant];
- if (isNaN(avg)) {
- avg = 0;
- }
- console.log(
- `- ${plant}; Rarity: ${plants[plant]}; Rating: ${avg.toFixed(2)}`
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment