Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function plantDiscovery(input) {
- let plantsList = {}
- let numberLine = input.shift()
- input.forEach((element, i) => {
- if (i < numberLine) {
- let [namePlant, rarity] = element.split("<->")
- if (!plantsList.hasOwnProperty(namePlant)) {
- rarity = Number(rarity)
- plantsList[namePlant] = { rarity, rating: [] }
- }
- }
- });
- let commandLine = input.slice(numberLine, input.indexOf("Exhibition"))
- commandLine.forEach(line => {
- let [command, ...rest] = line.split(": ")
- let [plant, data] = rest[0].split(" - ")
- data = Number(data)
- if (plantsList.hasOwnProperty(plant)) {
- switch (command) {
- case 'Rate':
- let raiting = Number(data)
- rate(plant, raiting)
- break;
- case 'Update': update(plant, data); break;
- case 'Reset': reset(plant); break;
- }
- } else {
- console.log("error")
- }
- })
- function rate(a, b) {
- plantsList[a].rating.push(b)
- }
- function update(a, b) {
- plantsList[a].rarity = b
- }
- function reset(a) {
- plantsList[a].rating = []
- }
- if (Object.entries(plantsList).length !== 0) {
- console.log(`Plants for the exhibition:`)
- for (let key in plantsList) {
- let avarage = plantsList[key].rating.reduce((a, b) => (a + b), 0)
- let finalResult = avarage / (plantsList[key].rating).length || 0
- console.log(`- ${key}; Rarity: ${plantsList[key].rarity}; Rating: ${finalResult.toFixed(2)}`)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement