divanov94

Untitled

Sep 21st, 2020 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(input){
  2.     let countPlants=Number(input.shift());
  3.     let obj={};
  4.     let plants=input.splice(0,countPlants);
  5.  
  6.     for (let plant of plants){
  7.         let [name,rarity]=plant.split("<->");
  8.         rarity=Number(rarity);
  9.  
  10.         if(!obj.hasOwnProperty(name)){
  11.             obj[name]= {
  12.                 rarity,
  13.                 rating:[]
  14.  
  15.             }
  16.         }
  17.     }
  18.    let commands=input.splice(0,input.indexOf("Exhibition"));
  19.    for(let line of commands){
  20.        let [command,lineOth]=line.split(": ");
  21.        let [plantName,value]=lineOth.split(" - ");
  22.        value=Number(value);
  23.  
  24.  
  25.  
  26.        switch(command){
  27.            case "Rate":
  28.                if(!obj.hasOwnProperty(plantName)){
  29.                    console.log(`error`);
  30.  
  31.                }else {
  32.                    obj[plantName].rating.push(value);
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.                }
  43.  
  44.  
  45.                break;
  46.            case "Update":
  47.                if(!obj.hasOwnProperty(plantName)){
  48.                    console.log(`error`);
  49.                }else {
  50.                   obj[plantName].rarity=value;
  51.  
  52.  
  53.                }
  54.                break;
  55.            case "Reset":
  56.                if(!obj.hasOwnProperty(plantName)){
  57.                    console.log(`error`);
  58.                }else {
  59.                    obj[plantName].rating=0;
  60.                }
  61.                break;
  62.  
  63.        }
  64.    }
  65.     let sorted=Object.entries(obj).sort((a,b)=>b[1].rarity-a[1].rarity || b[1].rating-a[1].rating);
  66.  
  67.     console.log(`Plants for the exhibition:`)
  68.     for(let kvp of sorted){
  69.         console.log(`- ${kvp[0]}; Rarity: ${kvp[1].rarity}; Rating: ${Number(kvp[1].rating).toFixed(2)}`)
  70.  
  71.     }
  72.  
  73. }
Add Comment
Please, Sign In to add comment