Advertisement
divanov94

Untitled

Aug 9th, 2020
62
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.        let avg=0;
  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.                    let avgArr=Array.from(obj[plantName].rating);
  34.                    avgArr.forEach(elem=>{
  35.                        avg+=elem/avgArr.length;
  36.                    })
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.                }
  44.  
  45.  
  46.                break;
  47.            case "Update":
  48.                if(!obj.hasOwnProperty(plantName)){
  49.                    console.log(`error`);
  50.                }else {
  51.                   obj[plantName].rarity=value;
  52.  
  53.  
  54.                }
  55.                break;
  56.            case "Reset":
  57.                if(!obj.hasOwnProperty(plantName)){
  58.                    console.log(`error`);
  59.                }else {
  60.                    delete obj[plantName].rating;
  61.                }
  62.                break;
  63.  
  64.        }
  65.    }
  66.  
  67.    console.log(obj)
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement