Advertisement
Liliana797979

plant discovery - final exam - fundamentals

Aug 4th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     // parse first part of input, creating initial plant catalog
  3.     // - determine how many element of the input represent the catalog
  4.     // - for each element of the input, create new catalog entry or update existing entry
  5.  
  6.     let actions = {
  7.         'Rate': rate,
  8.         'Update': update,
  9.         'Reset': reset
  10.     };
  11.     let catalog = {};
  12.  
  13.     let n = Number(input.shift());
  14.  
  15.     for (let i = 0; i < n; i++) {
  16.         let [name, rarity] = input.shift().split('<->');
  17.  
  18.         catalog[name] = {
  19.             rarity: Number(rarity),
  20.             ratings: [],
  21.             avgRating: 0
  22.         };
  23.     }
  24.  
  25.     // parse second part of input, updating the catalog, according to commands
  26.     // - for each remaining input element, until 'Exhibition' is reached:
  27.     // -- parse input, to determine command
  28.     // -- perform given command with given parameters
  29.  
  30.     while (input[0] != 'Exhibition') {
  31.         let [command, params] = input.shift().split(': ');
  32.         let action = actions[command];
  33.         action(params);
  34.     }
  35.  
  36.     // sort catalog
  37.     // - convert catalog to array
  38.     // - pass compare function into sorting operation
  39.     // -- return difference between plats' rarity
  40.     // -- if rarity is the same, return difference between plants' average rating
  41.     let sorted = Object.entries(catalog).sort(comparePlants);
  42.  
  43.     // print result
  44.     // - print static line
  45.     // - for each plant of the sorted array, print stats
  46.     console.log('Plants for the exhibition:');
  47.     for (let [name, plant] of sorted) {
  48.         console.log(`- ${name}; Rarity: ${plant.rarity}; Rating: ${plant.avgRating.toFixed(2)}`);
  49.     }
  50.  
  51.     function comparePlants(a, b) {
  52.         let rarityA = a[1].rarity;
  53.         let rarityB = b[1].rarity;
  54.  
  55.         let ratingA = a[1].avgRating;
  56.         let ratingB = b[1].avgRating;
  57.  
  58.         return (rarityB - rarityA) || (ratingB - ratingA);
  59.     }
  60.  
  61.     // Rate
  62.     // - parse parameters to determine plant name and given rating
  63.     // - if the plant exists in the catalog, add given rating (as number)
  64.  
  65.     function rate(line) {
  66.         let [name, rating] = line.split(' - ');
  67.         if (catalog[name] != undefined) {
  68.             let plant = catalog[name];
  69.             plant.ratings.push(Number(rating));
  70.  
  71.             let total = 0;
  72.             for (let rating of plant.ratings) {
  73.                 total += rating;
  74.             }
  75.             plant.avgRating = total / plant.ratings.length;
  76.  
  77.         } else {
  78.             console.log('error');
  79.         }
  80.     }
  81.  
  82.     // Update
  83.     // - parse parameters to determine plant name and given rarity
  84.     // - if the plant exists in the catalog, update its rarity (as number)
  85.  
  86.     function update(line) {
  87.         let [name, rarity] = line.split(' - ');
  88.         if (catalog[name] != undefined) {
  89.             let plant = catalog[name];
  90.             plant.rarity = Number(rarity);
  91.         } else {
  92.             console.log('error');
  93.         }
  94.     }
  95.  
  96.     // Reset
  97.     // - if the plant exists in the catalog, remove all ratings
  98.     function reset(name) {
  99.         if (catalog[name] != undefined) {
  100.             let plant = catalog[name];
  101.             plant.ratings.length = 0;
  102.             plant.avgRating = 0;
  103.         } else {
  104.             console.log('error');
  105.         }
  106.     }
  107. }
  108.  
  109. solve(['3',
  110.     'Arnoldii<->4',
  111.     'Woodii<->7',
  112.     'Welwitschia<->2',
  113.     'Rate: Woodii - 10',
  114.     'Rate: Welwitschia - 7',
  115.     'Rate: Arnoldii - 3',
  116.     'Rate: Woodii - 5',
  117.     'Update: Woodii - 5',
  118.     'Reset: Arnoldii',
  119.     'Exhibition']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement