Advertisement
bebo231312312321

Untitled

Apr 1st, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(input) {
  2.     let plantsList = {}
  3.     let numberLine = input.shift()
  4.     input.forEach((element, i) => {
  5.  
  6.         if (i < numberLine) {
  7.  
  8.             let [namePlant, rarity] = element.split("<->")
  9.             if (!plantsList.hasOwnProperty(namePlant)) {
  10.                 rarity = Number(rarity)
  11.  
  12.                 plantsList[namePlant] = { rarity, rating: [] }
  13.             }
  14.         }
  15.  
  16.     });
  17.     let commandLine = input.slice(numberLine, input.indexOf("Exhibition"))
  18.  
  19.     commandLine.forEach(line => {
  20.  
  21.         let [command, ...rest] = line.split(": ")
  22.  
  23.         let [plant, data] = rest[0].split(" - ")
  24.         data = Number(data)
  25.  
  26.         if (plantsList.hasOwnProperty(plant)) {
  27.             switch (command) {
  28.  
  29.                 case 'Rate':
  30.                     let raiting = Number(data)
  31.                     rate(plant, raiting)
  32.                     break;
  33.  
  34.                 case 'Update': update(plant, data); break;
  35.                 case 'Reset': reset(plant); break;
  36.             }
  37.         } else {
  38.             console.log("error")
  39.         }
  40.     })
  41.     function rate(a, b) {
  42.         plantsList[a].rating.push(b)
  43.     }
  44.     function update(a, b) {
  45.    
  46.         plantsList[a].rarity = b
  47.     }
  48.     function reset(a) {
  49.         plantsList[a].rating = []
  50.     }
  51.  
  52.  
  53.     if (Object.entries(plantsList).length !== 0) {
  54.         console.log(`Plants for the exhibition:`)
  55.         for (let key in plantsList) {
  56.            
  57.             let avarage = plantsList[key].rating.reduce((a, b) => (a + b), 0)
  58.             let finalResult = avarage / (plantsList[key].rating).length || 0
  59.             console.log(`- ${key}; Rarity: ${plantsList[key].rarity}; Rating: ${finalResult.toFixed(2)}`)
  60.         }
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement