bebo231312312321

Untitled

Mar 30th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(input) {
  2.     let plantsList = {}
  3.     let plantLine = input.splice(0, input.shift()).forEach((el) => {
  4.         let [namePlant, rarity] = el.split("<->").map(x => isNaN(x) ? x : Number(x))
  5.         plantsList[namePlant] = { rarity, rating: [] }
  6.     });
  7.     let commandLine = input.slice(0, input.indexOf("Exhibition")).forEach(line => {
  8.         let [command, ...rest] = line.split(": ")
  9.         let [plant, data] = rest[0].split(" - ").map(x => isNaN(x) ? x : Number(x))
  10.         if (plantsList.hasOwnProperty(plant)) {
  11.             switch (command) {
  12.                 case 'Rate': rate(plant, data); break;
  13.                 case 'Update': update(plant, data); break;
  14.                 case 'Reset': reset(plant); break;
  15.             }
  16.         } else {console.log("error")}
  17.     })
  18.     function rate(a, b) { plantsList[a].rating.push(b) }
  19.     function update(a, b) { plantsList[a].rarity = b }
  20.     function reset(a) { plantsList[a].rating = [] }
  21.     console.log(`Plants for the exhibition:`)
  22.     for (let key in plantsList) {
  23.         let avarage = plantsList[key].rating.reduce((a, b) => (a + b), 0)
  24.         let finalResult = avarage / (plantsList[key].rating).length || 0
  25.         console.log(`- ${key}; Rarity: ${plantsList[key].rarity}; Rating: ${finalResult.toFixed(2)}`)
  26.     }
  27. }
Add Comment
Please, Sign In to add comment